PHP 5/MySQL Programming- P44

PHP 5/MySQL Programming- P44:computer programming has often been seen as a difficult and arcane skill. Programming languages are difficult and complicated, out of the typical person’s reach. However, the advent of the World Wide Web has changed that to some extent. It’s reasonably easy to build and post a Web page for the entire world to see. The language of the Web is reasonably simple, and numerous applications are available to assist in the preparation of static pages | 193 FIGURE The program shows what would happen if Shakespeare were a cartoon character. Chapter 6 Working th Files foreach sonnet as currentLine currentLine str_replace r w currentLine currentLine str_replace l w currentLine output . rtrim currentLine . br n end foreach output . You wascally wabbit br n print output body html Loading the File into an Array with file Some shortcut file-handling tricks do not require you to create a file pointer. You might recall the readFile command from chapter 1 Exploring the PHP Environment. That file simply reads a file and echoes it to the output. The file command is similar because it does not require a file pointer. It opens a file for input and returns an array with each file line occupying one array element. This can make file work easy because you can use a foreach loop to step through each line and perform modifications. 194 PHP 5 MySQL Programming for the Absolute Beginner Reading a file into an array is attractive because it s easy you can quickly work with the file once it s in memory. The problem comes when working with very large files. The computer s memory is finite and large files can cause problems. For larger data files try a one-line-at-a-time approach using the fgets function inside a loop. Using str_replace to Modify File Contents Inside the foreach loop it s a simple matter to convert all occurrences of r and l to the letter w with the str_replace function. The resulting string is added to the output variable which is ultimately printed to the screen. f In the Real World This particular application is silly and pointless but the ability to replace all occurrences of one string with another in a text file is useful in a variety of circumstances. For example you could replace every occurrence of the left brace character in an HTML document with the lt sequence. This results in a source code listing that s directly viewable on the browser. You might use such technology for form letters .

Không thể tạo bản xem trước, hãy bấm tải xuống
439    38    5    31-03-2023
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.