Friday, December 17, 2010

PHP - String

$quotes = "The world doesn't need a copy of the same thing";

To get the length of the string use:
>> $length = strlen($quotes);     // result is 41

To get the start position of the specified word, use:
>> $pos = strpos($quotes,"world");     // result is 4

To split the string to array, use:
>> print_r(explode(" ",$quotes));     // space as separator

To display raw html code as is, use:
>> $text = htmlspecialchars("<h1>Holla World<h1>");

To repeat string at specified times:
>> $dots = str_repeat(".",10);      // repeat dot for 10 times

To replace a specified string in string:
It will result: "new to new"
>> $newString = str_replace("old","new","old to new");  

To count words in $quote, use:
>> $count = str_word_count($quotes);     //  result in 10


No comments:

Post a Comment