Remove a string from the beginning of a string in PHP
In this post we will give you information about Remove a string from the beginning of a string in PHP. Hear we will give you detail about Remove a string from the beginning of a string in PHPAnd how to use it also give you demo for it if it is necessary.
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 substr
In 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,,strlen($conuntry_code))==$conuntry_code){
- $number=substr($number,strlen($conuntry_code));
- }
- echo$number;
$conuntry_code = '91'; $number = '918888888888'; if (substr($number, 0, strlen($conuntry_code)) == $conuntry_code) { $number = substr($number, strlen($conuntry_code)); } echo $number;
Example 1: Using ltrim
- $conuntry_code='91';
- $number='918888888888';
- echoltrim($number,$conuntry_code);
$conuntry_code = '91'; $number = '918888888888'; echo ltrim($number,$conuntry_code);
Using ltrim function, you can remove whitespace or other characters from the left side of a string.
Using substr function, you can cut a part of a string from a string.
Using strlen function, you can get length of a string.
Hope this code and post will helped you for implement Remove a string from the beginning of a string in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs