$haystack = 'File_get_contents() is easier than using fopen().';

 $result = preg_replace($regex, $replace, $haystack);

 echo $result;

?>

The $1 is our backreference; it will be substituted with the results from the first subexpression. The way we have written the regular expression is very exact. The [A-Za-z0-9_]* part, which matches the function name, is marked as a subexpression. After that is (), which means the exact symbols (and), not the regular expression meanings of them, which means that $1 in the replacement will contain fopen rather than fopen(), which is how it should be. Of course, anything that is not backreferenced in the replacement is removed, so we have to put the () after the first $1 (not in the hyperlink) to repair the function name.

After all that work, the output is perfect:

<em>File_get_contents()</em> (<a href='http://www.php.net/file_get_contents'>manual</A>) is easier than using <em>fopen() </em> (<a href='http://www.php.net/fopen'>manual</A>).

Handling HTML Forms

Given that PHP's primary role is handling web pages, you might wonder why this section has been left so late in the chapter. It is because handling HTML forms is so central to PHP that it is essentially automatic.

Consider this form:

<form method='POST' action='thispage.php'>

 User ID: <input type='text' name='UserID' /><br />

 Password: <input type='password' name='Password' /><br />

 <input type='submit' />

</form>

When a visitor clicks the Submit button, thispage.php is called again and this time PHP has the variables available to it inside the $_REQUEST array. Given that script, if the user enters 12345 and frosties as her user ID and password, PHP provides you with $_REQUEST['UserID'] set to 12345 and $_REQUEST['Password'] set to frosties. Note that it is important that you use HTTP POST unless you specifically want GET. POST enables you to send a great deal more data and stops people from tampering with your URL to try to find holes in your script.

Is that it? Well, almost. That tells you how to retrieve user data, but be sure to sanitize it so that users do not try to sneak HTML or JavaScript into your database as something you think is innocuous. PHP gives you the strip_tags() function for this purpose. It takes a string and returns the same string with all HTML tags removed.

Reference

Being as popular as it is, PHP gets a lot of coverage on the Internet. The best place to look for information, though, is the PHP online manual, at http://www.php.net/. It is comprehensive, well-written, and updated regularly:

http://www.phpbuilder.net/ — A large PHP scripts and tutorials site where you can learn new techniques and also chat with other PHP developers.

http://www.zend.com/ — The home page of a company founded by two of the key developers of PHP. Zend develops and sells proprietary software, including a powerful IDE and a code cache, to aid PHP developers.

http://pear.php.net/ — The home of the PEAR project contains a large collection of software you can download and try, and the site has thorough documentation for it all.

http://www.phparch.com/ — There are quite a few good PHP magazines around, but PHP Architect probably leads the way. It posts some of its articles online for free, and its forums are good, too.

Quality books on PHP abound, and you are certainly spoiled for choice. For beginning developers, the best available is PHP and MySQL Web Development (Sams Publishing), ISBN 0-672-32672-8. For a concise, to-the-point book covering all aspects of PHP, check out PHP in a Nutshell (O'Reilly). Finally, for advanced developers, you can consult Advanced PHP Programming (Sams Publishing), ISBN 0-672-32561-6.

CHAPTER 28

C/C++ Programming Tools for Fedora

If you're looking to learn C or C++ programming, this part of the book isn't the right place to start — unlike Perl, Python, PHP, or even C#, it takes more than a little dabbling to produce something productive with C, so this chapter is primarily focused on the tools Fedora offers you as a C or C++ programmer.

Whether you're looking to compile your own code or someone else's, the GNU Compiler Collection (gcc) is there to help — it understands C, C++, Fortran, Pascal, and dozens of other popular languages, which means you can try your hand at whatever interests you. Fedora also ships with hundreds of libraries you can link to, from the GUI toolkits behind GNOME and KDE to XML parsing and game coding. Some use C, others C++, and still others offer support for both, meaning you can choose what you're most comfortable with.

Programming in C/C++ with Linux

C is the programming language most frequently associated with Unix-like operating systems such as Linux or BSD. Since the 1970s, the bulk of the Unix operating system and its applications have been written in C. Because the C language doesn't directly rely on any specific hardware architecture, Unix was one of the first portable operating systems. In other words, the majority of the code that makes up Unix doesn't know and doesn't care on which computer it is actually running. Machine-specific features are isolated in a few modules within the Unix kernel, which makes it easy for you to modify them when you are porting to different hardware architectures.

C is a compiled language, which means that your C source code is first analyzed by the preprocessor. It is then translated into assembly language and then into machine instructions that are appropriate to the target CPU. An assembler then creates a binary, or object, file from the machine instructions. Finally, the object file is linked to any required external software support by the linker. A C program is stored in a text file that ends with a .c extension and always contains at least one routine, or function, such as main() , unless the file is an include file (with an .h extension — also known as a header file) containing shared variable definitions or other data or declarations. Functions are the commands that perform each

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату