im trying to get preg_replace to totally remove a string from a message
heres an example of a $message:
hello there john214
welcome to the site!
please get feel free to kick back and relax.
where john214 is a username outputted via the db
now i want to get rid of hello there john214 using preg_replace, however, my code is removing everything except for john214
heres my code:
$message = preg_replace('|hello there (.*?)|si', '', $message);
why is it not removing john214?
It might be because you are doing a non greedy approach. Try this:
This will get rid of EVERYTHING that goes after
hello there(because of the S modifier). However, if what you want is get rid of everything in a single line that goes afterhello there, you want to get rid of thesmodifier as:If you use the s modifier you are making the Dot metacharacter to match newlines, and that’s why the first regex will consume all what goes after hello there (even jumping to new lines).