Searching strings
some thing practical
PHP provides you several useful function for searching strings including strstr(), strpos(),strrpos(), substr_count() and strpbrk().
Checking if a string contains a substring
To check if a string contains a particular substring, you use the strstr() function. The strstr()accepts two parameters: a string that to be checked and a substring that you want to search for. If the substring found, the function returns the portion of the string that begins of the found text to the end of the string. It returns false if the substring cannot be found.
<?php
$str = 'Find a substring in a string';
$sub_str = 'substring';
echo strstr($str,$sub_str); //substring in a string?>
Locating position of a substring within a PHP string
If you want to know exact position of a substring within a string, you use the strpos() function. It returns the first occurrence of a substring in a string.
<?php
$str = 'Using strpos function in PHP';
$sub_str = 'strpos';
echo strpos($str,$sub_str); // 6
?>
The strrpos() function is similar to the strpos() function except it returns the position of the last occurrence of a substring in a string.
<?php
$str = 'String function';
$sub_str = 'n';
echo strpos($str,$sub_str); // 4
echo strrpos($str,$sub_str); // 14
?>
Getting the number of occurrences of a substring
To get how many times a substring that appears in a string, you use the substr_count() function. The substr_count() function is useful in many cases e.g., you can count how many times a keyword is mentioned in an article to calculate its density.
<?php
$str = <<<EOT
This is sample text that demonstrates
how to calulate keyword density in
an article. We can use this function
for on-page optimization function in
SEO software.
EOT;
$keyword = 'function';
$word_count = str_word_count($str);
$keyword_count = substr_count($str,$keyword);
if($word_count > 0){
$density = $keyword_count * 100 / $word_count;
echo number_format($density, 2, '.', '') . '%'; //7.69%
}?>
Checking if a string contains any character in a set of characters
To check if a string contains any character in a set of characters, you use strpbrk() function. The strpbrk() function accepts two parameters: a string and a set of characters that you want to find. It returns a substring starting from the character found or false if character in a set of characters does not appear in the string. The strbbrk() function is useful in many cases e.g., you can check if a user name contains any special character in a set !, @, $, %… as follows:
<?php
$username1 = 'zentut';
$username2 = 'zen@tut';
$not_allowed_chars = '!@#$%^&*';
if(strpbrk($username1, $not_allowed_chars)){
echo 'invalid username because it contains one of ' . $not_allowed_chars;
}
if(strpbrk($username2, $not_allowed_chars)){
echo 'invalid username because it contains one of ' . $not_allowed_chars;
}?>
Replacing in strings
PHP provides some useful string functions to replace some portions of a string with different strings including str_replace(), substr_replace() and strst().
Replacing all occurrences of a search string
To replace all occurrences of a search string with the replacement string, you use the str_replace()function. Let’s take a look at the following example:
<?php
$str = 'This is an example of a string
that demonstrates string replacement';
echo str_replace('string', 'text', $str);
// This is an example of a text
// that demonstrates text replacement
?>
We replaced all occurrences of ‘ string‘ by ‘ text‘ in the string $str.
To count how many times the search string was replaced, you need to pass the fourth argument to the function as follows:
<?php
$str = 'This is an example of a string
that demonstrates string replacement';
$times = 0;
str_replace('string', 'text', $str,$times);
echo "The string was replaced $times time(s)";
?>
Removing whitespace
To remove whitespace from:
the beginning of a string you use the ltrim() function.
the end of the string you use the rtrim() function
both the beginning and the end of a string you use the trim() function.
Let’s take a look at the following example:
<?php
$s1 = ' a string with whitespace at the beginning';
$s2 = 'a string with whitespace at the end ';
$s3 = ' a string with whitespaces at the beginning and the end ';
echo ltrim($s1) . '<br/>';
echo rtrim($s2) . '<br/>';
echo trim($s3) . '<br/>';
?>
With those functions, you can remove other characters other than whitespace if you specify them as the second argument.
No comments:
Post a Comment