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
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.