▼  Site Navigation Main Articles News Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc Links to Webcomics Bible Quotes About Older Musings
site version 7.3
PHP –– String Functions
written by: admin


Date Written: 8/11/07 Last Updated: 1/15/10

PHP has a lot of built in string functions.  While PHP.net gives some great descriptions of them, sometimes they are a bit difficult to understand or else they don't give an explanation that is detailed enough for me.  That is why I am adding this thread.  It is a quick reference for myself and will probably make more sense to me than the explanations at php.net, but only because I am not as familiar with PHP as I could be.

strtr($str,from,to).

In order to replace specific letters with their uppercase versions or vice versa irrespective of their locations in the script you will need to go back to the built in PHP functions available.

$string="this is a good fish.";
$string = strtr($string,asdf,ASDF);
echo "$string";
//prints "thiS iS gooD FiSh."


other case converting functions include:

ucfirst() capitalize the first letter in a string,
ucwords() capitalize the first letter of every word,
strtolower() convert all letters in a string to lowercase, and
strtoupper() convert all letters in a string to uppercase.

Some applications for these case transforming functions that I can think of include the formatting of passwords or addresses or names or sentence structure.  With sentence structure you can split the string into an array of sentences and use ucfirst() to capitalize the first letter of every sentence.

strpos()

I found a variation of this one on php.net.  It searches a string for a variable and says if and where it was found.
<?php
$mystring = 'abc';
$findme   = 'd';
$pos = strpos($mystring, $findme);
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>


substr() copy a specific segment of a string.

$piece = strpos($complete,0,1);

assigns the first character in $complete to the string $a.

TAGS: php
copyright 2005–2024