In this post, i will tell you how to remove a string from beginning of a string in PHP.
Sometime you have a prefix in a variable and you need to remove if given string has prefix then these examples will help you to find out if prefix exist in given string then it will remove it from string.
Example 1: Using substrIn this example, i have a number with country code and i want to get only number without country code then first i check if number has country code then remove it from number.
- $conuntry_code = '91';
- $number = '918888888888';
- if (substr($number, 0, strlen($conuntry_code)) == $conuntry_code) {
- $number = substr($number, strlen($conuntry_code));
- }
- echo $number;
- $conuntry_code = '91';
- $number = '918888888888';
- echo ltrim($number,$conuntry_code);