Common PHP string processing function usage example [conversion, escape, truncation, comparison, search, reverse, CUT], example escape
This article analyzes common PHP string processing function usage. We will share this with you for your reference. The details are as follows:
<? Php $ s = "hello world"; // sort out echo 'trim (); ltrim (); rtrim () '; echo' <br/> '; echo'. The length is: '. strlen ($ s); echo '<br/>'; // upper case echo 'Upper case :'. ucfirst ($ s); echo '<br/>'; echo 'Upper-case letters of each word :'. ucwords ($ s); echo '<br/>'; echo 'Upper case :'. strtoupper ($ s); echo '<br/>'; echo 'lower case :'. strtolower ($ s); echo '<br/>'; // escape string function. Before saving data to the database, use echo 'addslashes (); stripslashes ();'; $ s1 = '"a" B'; echo '<br/>'; echo addslashes ($ s1); echo '<br/> '; // Capture echo substr ($ s, 4, 4); echo' <br/> '; $ s2 = "hello world"; $ s3 = "test "; // compare, return 0 echo strcmp ($ s, $ s2) = 0? "Equal": "Unequal"; echo '<br/>'; echo strcmp ($ s, $ s3) = 0? "Equal": "Unequal"; echo '<br/>'; // find echo strpos ($ s, 'O'); echo '<br/> '; echo strrpos ($ s, 'O'); // Note: If not found, false = 0 is returned, therefore, use "=" to check if (XX = false); echo '<br/>'; // reverse echo strrev ($ s ); echo '<br/>'; // cut $ arr = str_split ($ s); $ arr1 = str_split ($ s, 2); $ arr2 = explode ('', $ s); var_dump ($ arr); var_dump ($ arr1); var_dump ($ arr2);?>