//String Functions
//str_word_count(); //the 2nd argument z optional
$str = 'my name is waqas.';
$str_word_count = str_word_count($str, 0);
echo 'the string has <b>'.$str_word_count .'</b> words.</br>';
//printing in an array
//str_word_count($str1, 1};
$str1 = 'my name is waqas.';
$an_array = str_word_count($str1, 1);
print_r($an_array);
echo '</br>';
//pirnting in an associative array
//str_word_count($str2, 2);
$str2 = 'my name is waqas .';
$an_associative_array = str_word_count($str2, 2, '.');
print_r($an_associative_array);
//string shuffle
//str_shuffle();
$str3 = 'my name is waqas.';
$shuffle = str_shuffle($str3);
echo $shuffle.'</br>';
//sub string
//substr($str4, argument);
$str4= 'my name is waqas';
$sub_string = substr($str4, 4);
echo $sub_string.'</br>';
//string lenth
//strlen();
$str5 ='my name is waqas.';
$str_lenth =strlen($str5);
echo 'string lenth '.$str_lenth.'</br>';
//string lenth with for loop
$string_w = 'waqa s';
$string_l = strlen($string_w);
for ($x=1; $x<=$string_l; $x++) {
echo $x.' ';
}
//A program ussing string shuffle, sub string & string lenth
$str6 = 'my name is waqas';
$shuf = str_shuffle($str6);
$half = substr($shuf, 0, strlen($str6)/4); //substr($shuf, 0, 10);
echo $half.'</br>';
//string revers
//strrev();
$str7 = 'my name is waqas';
$str_revers = strrev($str7);
echo $str_revers.'</br>';
//similar text
//similar_text();
$str_a = 'my name is waqas..';
$str_b = 'my last name is khan.';
similar_text ($str_a, $str_b, $rsut);
echo 'The similarty is:<b> '.$rsut,' %</b></br>';
//String Functions: Upper / Lower Case Conversion
//strtoupper();, &strtolower();
$upper = 'upper case';
$string_uc = strtoupper($upper);
echo '<b>'.$string_uc.'</b></br>';
$lower = 'LOWER CASE';
$string_lc = strtolower($lower);
echo '<i>'.$string_lc.'</i></br>';
//string trim
//trim();, ltrim();, &rtrim();
//add slashes
//addslashes(); & stripslashes();
$code = 'my name is<img src="image.jpg"> waqas';
$add_slashes = htmlentities(addslashes($code));
echo stripslashes($add_slashes);
//Expression matchion
//preg_match('/word/', $a1);
$a1 = 'find the word';
if(preg_match('/word/', $a1)) {
echo 'Match found</br>';
} else {
echo 'Match not found</br>';
}
//preg_match: another program with function
function has_space($a2) {
if(preg_match('/ /', $a2)) {
return true;
} else {
return false;
}
}
if (has_space('mynameiswaqas')) {
echo '<b>line has space</b></br>';
} else {
echo '<b>line has no space</b></br>';
}
//string positiion function
//strpos($b1, $find, $offset); //offset is optional
$expl = 'my name is waqas';
$loc_pos= strpos($expl, 'is');
echo $loc_pos.'</br>';
//string position with while loop
$offset = 0;
$find ='p';
$find_len = strlen($find);
$b1 = 'This is my php file';
while($find_pos = strpos($b1, $find, $offset)) {
echo '<b>'.$find.' </b>find at: '.$find_pos.'</br>';
$offset = $find_pos + $find_len;
}
//String Functions: Replacing Part of a String
//substr_replace($c1, replace, start from, chr leth);
$c1 = 'my name is waqas & my last name is khan';
$string_new = substr_replace($c1, 'and', 17, 1);
echo $string_new.'</br>';
//String Functions: Replacing Predefined Part of a String
//str_replace(looking for, replace with, string);
$find_w = array('1946', 'bad', 'no');
$replace_w = array('1947', 'good', 'on');
$a2 = 'Pakistan came in to being no 1946 and that is bad';
$str11 = str_replace($find_w, $replace_w, $a2);
echo $str11;