| AND OR | string words | *

string handling

|

substring examples

| see demonstration of substring examples


some examples using substr(),
extracting the first, last, n chars of a string.
reverse a string or see how many times a char is used in a string.



full source of substring examples [ line 1 - 29 ] | download substring examples

1    <?php
2   echo '
3   <form method="post" action="'.$_SERVER['PHP_SELF'].'">
4       <input type="text" name="word">
5       <input type="submit" value="see">
6   </form>';
7   if(!empty($_POST['word']))
8   {
9       $str $_POST['word'];
10       $char['original'] = $str;
11       $char['words'] = explode(' ',$str);
12       $char['word_count'] = count($char['words']);
13       $char['first_char'] = substr($str,0,1);
14       $char['last_char'] = substr($str,-1);
15       $char['all_but_first_char'] = substr($str,1);
16       $char['all_but_last_char'] = substr($str,0,-1);
17       for($x 0$x strlen($str); $x++)
18       {
19           $char['chars'][] = substr($str,$x,1);
20           $char['char_repeat'][substr($str,$x,1)]++;
21       }
22       $char['char_count'] = strlen($str);
23       ksort($char['char_repeat']);
24       $char['reverse'] = join('',array_reverse($char['chars']));
25       echo '<pre>';
26       print_r($char);
27       echo '</pre>';
28   }
29   ?>



14 hits by 7 users in the last 30 minutes.