communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "string handling" - download "substring examples" - open demo of "substring examples"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

string handling - substring examples was created/last modified on Fri 16 Jul 2004 8:16 AM

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.

<?php

echo '
<form method="post" action="'
.$_SERVER['PHP_SELF'].'">
     <input type="text" name="word">
     <input type="submit" value="see">
</form>'
;

if(!empty(
$_POST['word']))
{
     
$str = $_POST['word'];

     
$char['original'] = $str;

     
$char['words'] = explode(' ',$str);

     
$char['word_count'] = count($char['words']);

     
$char['first_char'] = substr($str,0,1);

     
$char['last_char'] = substr($str,-1);

     
$char['all_but_first_char'] = substr($str,1);

     
$char['all_but_last_char'] = substr($str,0,-1);

     for(
$x = 0; $x < strlen($str); $x++)
     {
         
$char['chars'][] = substr($str,$x,1);

         
$char['char_repeat'][substr($str,$x,1)]++;
     }

     
$char['char_count'] = strlen($str);

     
ksort($char['char_repeat']);

     
$char['reverse'] = join('',array_reverse($char['chars']));

     echo
'<pre>';
     
print_r($char);
     echo
'</pre>';
}

?>

learn more about some of the PHP functions used in this snippet: array, array_reverse, count, echo, empty, explode, extract, for, join, ksort, ord, print, print_r, sort, strlen, substr, time

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari