076_String_Length_&_Count11
4
3
2
Array
(
[0] => rajat
[6] => singh
)
076-a - substr_count, count specific word in the string2
1
1
0
<?php
// 076 - String_Length_&_Count
echo "</br><span class='myheading'>076_String_Length_&_Count</span></br>";
$str = "rajat singh";
$newStr = strlen($str); // count # of characters
$str1 = "Baba";
$newStr1 = strlen($str1); // count without space
$str2 = "Yah oo Baba";
$newStr2 = str_word_count($str2); // count # of words
echo '<pre>';
print_r($newStr)."<br>";
echo '</pre>';
echo '<pre>';
print_r($newStr1)."<br>";
echo '</pre>';
echo '<pre>';
print_r($newStr2)."<br>";
echo '</pre>';
$array = str_word_count($str,0); // default
$array1 = str_word_count($str,2); // create array of 2 value(words) with their keys
echo "<pre>";
print_r($array);
echo "</pre>";
echo "<pre>";
print_r($array1);
echo "</pre>";
// 076-a - substr_count, count specific word in the string, case sensitive
echo "</br><span class='myheading'>076-a - substr_count, count specific word in the string</span></br>";
$str5 = "Hello world. The world is nice";
$count = substr_count($str5,"world"); // count world in the string
echo "<pre>";
print_r($count);
echo "</pre>";
$count = substr_count($str5,"world",10); // count form 10th position
echo "<pre>";
print_r($count);
echo "</pre>";
$count = substr_count($str5,"world",1,20); // count form 1 to 20th position
echo "<pre>";
print_r($count);
echo "</pre>";
$count = substr_count($str5,"world",1,5); // count form 1 to 5th position
echo "<pre>";
print_r($count);
echo "</pre>";
?>