25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Strings And Patterns ” 77<br />

Transforming a String<br />

The strtr() function can be used to translate certain characters of a string into other<br />

characters—it is often used as an aid in the practice known as transliteration to transform<br />

certain accented characters that cannot appear, for example, in URLs or e-mail<br />

address into the equivalent unaccented versions:<br />

// Single character version<br />

echo strtr (’abc’, ’a’, ’1’); // Outputs 1bc<br />

// Multiple-character version<br />

$subst = array (<br />

’1’ => ’one’,<br />

’2’ => ’two’,<br />

);<br />

echo strtr (’123’, $subst); // Outputs onetwo3<br />

Using Strings as Arrays<br />

You can access the individual characters of a string as if they were members of an<br />

array. For example:<br />

$string = ’abcdef’;<br />

echo $string[1]; // Outputs ’b’<br />

This approach can be very handy when you need to scan a string one character at a<br />

time:<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

$s = ’abcdef’;<br />

for ($i = 0; $i < strlen ($s); $i++) {<br />

if ($s[$i] > ’c’) {<br />

echo $s[$i];<br />

}<br />

}

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

Saved successfully!

Ooh no, something went wrong!