Commonly in interview we have been asked about reverse a string. So here I am going to share the solution for this type of question with my article. Image source : google images This is a basic question but most of the developers are stuck in it due to hesitation of interview. Interviewer only want to ask the main logic to reverse a string. He can either ask the function in php to reverse a string or also ask about procedural logic behind this. You can reverse a string using…
Category: php string
how to search a specific word in a string in php?
You can use the strpos function which is used to find the occurrence of one string inside other strpos() : strpos() function — Find the position of the first occurrence of a substring in a different string It’s support (PHP 4, PHP 5, PHP 7). Syntax : mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) Find the numeric position of the first occurrence of needle in the haystack string. Parameters : haystack The string to search in. needle If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. …
How to count the number of substring occurrences in a string ?
Common PHP issue that mostly developer face. below is the solution for such type of issue.. substr_count substr_count — Count the number of substring occurrences int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive. Note: This function doesn’t count overlapped substrings. See the example below! Parameters ¶ haystack The string to search in needle The substring to search for offset The offset where to start counting. If the offset is negative, counting starts from the end of the string. length The maximum length after the…
How to remove everything before a specific first character?
Sometime developers are facing the issue of eliminating all characters before the first specific character. Here I am describing how you can remove all the characters before a specific thing. Just suppose we have a string as below:- <?php $str = ‘http://blog.phpmypassion.com’; ?> Now If I want to get my main domain then I have to follow below code.. <?php if(($pos = strpos($str, ‘.’)) !== false) { $new_str = substr($str, $pos + 1); } else { $new_str = get_last_word($str); } echo ‘new string will be=’.$new_str; ?>…