Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove first n numeric characters from a string using PHP?

I have a string $str = "4sX3yY4mj2DT9gVOOS0x60onT08lwzLLZBqn8". Now I want to remove the first 5 numeric characters from the string.

How do I do that in php?

like image 559
MD. Atiqur Rahman Avatar asked Jan 16 '17 18:01

MD. Atiqur Rahman


People also ask

How do I remove the first character of a string in PHP?

In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known. $str = "geeks" ; // Or we can write ltrim($str, $str[0]); $str = ltrim( $str , 'g' );

How do I remove numbers and special characters from a string in PHP?

One useful function that can be used to remove special characters from a string is the str_replace() function. The empty string must be used to the replace character in this function to remove the specified character. The syntax of this function is given below. The str_replace() function can take four arguments.


1 Answers

Use the limit parameter with preg_replace:

$str = preg_replace('~\d~', '', $str, 5);
like image 147
Casimir et Hippolyte Avatar answered Oct 07 '22 21:10

Casimir et Hippolyte