skip to Main Content

I have tried to remove an email sign which is @ from a string like this:

    $string = @james_dy; //OR:
    $string = @smartsge130;

etc. using the followings:

    $string1 = preg_replace('/[^p{L}p{N}s]/u', '', $string);

Or:

    $string1 = str_replace('@', '', $string);

without a success.

Please advise how to do that.

2

Answers


  1. Chosen as BEST ANSWER

    I can confirm that both my codes work really well; they did not work previously due to a wrong variable used for showing after trying for an hour before posting for help here. But I prefer the second option removing only @. Many thanks


  2. Both your method (preg_replace and str_replace) work well.

    If you meet an error, it may be caused by syntax mistake, and it should be:

    $string = "@james_dy";
    

    One should keep in mind that PHP need strings in quotes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search