简体   繁体   中英

How to replace the first three character on a string

How to replace the first three special character in my string.? This is the sample.

$string = "~~MASTER FOR OILCHEM; ETD - MID OF JUNE~~";
 echo preg_replace("/[^a-zA-Z0-9]/", "", $string);

The outout will be :

MASTERFOROILCHEMETDMIDOFJUNE

But I want the fist three special character to be replace. So the output will be :

MASTER FOR OILCHEM; ETD - MID OF JUNE~~

Instead of regex you can simply use substr like as

$string = "~~MASTER FOR OILCHEM; ETD - MID OF JUNE~~";
echo substr($string,2);

The following code searches for 0 up to 3 non word characters and replaces them with an empty string:

$string = "~~MASTER FOR OILCHEM; ETD - MID OF JUNE~~";
echo preg_replace("/^\W{0,3}/", "", $string);

The above PHP outputs the following:

MASTER FOR OILCHEM; ETD - MID OF JUNE~~

See https://3v4l.org/lFKXV for a live example.

And regarding Regular Expressions, you should try this great tool.
https://regex101.com/r/pD7vR7/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM