13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introducing Regular Expressions<br />

123<br />

This function replaces all the instances of needle in haystack with new_needle <strong>and</strong><br />

returns the new version of the haystack.The optional fourth parameter, count, contains<br />

the number of replacements made.<br />

Note<br />

You can pass all parameters as arrays, <strong>and</strong> the str_replace() function works remarkably intelligently.<br />

You can pass an array of words to be replaced, an array of words to replace them with (respectively), <strong>and</strong> an<br />

array of strings to apply these rules to. The function then returns an array of revised strings.<br />

For example, because people can use the Smart Form to complain, they might use some<br />

colorful words. As a programmer, you can easily prevent Bob’s various departments from<br />

being abused in that way if you have an array $offcolor that contains a number of<br />

offensive words. Here is an example using str_replace() with an array:<br />

$feedback = str_replace($offcolor, ‘%!@*’, $feedback);<br />

The function substr_replace() finds <strong>and</strong> replaces a particular substring of a string<br />

based on its position. It has the following prototype:<br />

string substr_replace(string string, string replacement,<br />

int start, int [length] );<br />

This function replaces part of the string string with the string replacement.Which<br />

part is replaced depends on the values of the start <strong>and</strong> optional length parameters.<br />

The start value represents an offset into the string where replacement should begin.<br />

If it is zero or positive, it is an offset from the beginning of the string; if it is negative, it<br />

is an offset from the end of the string. For example, this line of code replaces the last<br />

character in $test with “X”:<br />

$test = substr_replace($test, ‘X’, -1);<br />

The length value is optional <strong>and</strong> represents the point at which <strong>PHP</strong> will stop replacing.<br />

If you don’t supply this value, the string will be replaced from start to the end of the<br />

string.<br />

If length is zero, the replacement string will actually be inserted into the string without<br />

overwriting the existing string. A positive length represents the number of characters<br />

that you want replaced with the new string; a negative length represents the point<br />

at which you would like to stop replacing characters, counted from the end of the string.<br />

Introducing Regular Expressions<br />

<strong>PHP</strong> supports two styles of regular expression syntax: POSIX <strong>and</strong> Perl. Both types are<br />

compiled into <strong>PHP</strong> by default, <strong>and</strong> as of <strong>PHP</strong> versions 5.3 the Perl (PCRE) type cannot

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!