Remove a string from the beginning of a string in PHP

Remove a string from the beginning of a string in PHP

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.

  1. $conuntry_code = '91';
  2. $number = '918888888888';
  3. if (substr($number, 0, strlen($conuntry_code)) == $conuntry_code) {
  4. $number = substr($number, strlen($conuntry_code));
  5. }
  6. echo $number;
Example 1: Using ltrim
  1. $conuntry_code = '91';
  2. $number = '918888888888';
  3. echo ltrim($number,$conuntry_code);

Phone: (+91) 8800417876
Noida, 201301
Betwinner
pendik escort
kartal escort