php - How to replace the first three character on a string

539

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~~
345

Answer

Solution:

Instead ofregex you can simply usesubstr like as

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

Answer

Solution:

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

People are also looking for solutions to the problem: php - How to hide get parameter from url in codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.