Sunday, January 2, 2011

PHP String Function

To get the length of the string, type:
$lol = "lady gaga";
echo strlen($lol);
output>> 9

To get part of the string,
echo substr("Holla World",6);    // substr(text, start)
output>> World
echo substr("Holla World",6,2);    // substr(text, start, length)
output>> Wo

To get the position of the specified string in a text, type:
echo strpos("Holla World","Wo"); // strpos(text,searched,[start])
output>> 6
echo strpos("Holla World","o",1);
output>> 2
To break a string into array, type:
print_r(explode(" ","Holla World"));    // explode(separator, 
                                        // string);
output>> Array ( [0] => Holla [1] => World ) 


1 comment: