The variable $description which contains some text may also contain a string like so:
!some_text_that_may_be_different_each_time!
I need to get rid of these, so I tried:
$new_description = preg_replace("!.+?!", '', $description);
But no luck. What would be a good way to get rid of these strings?
Also removing them may leave extra spaces. For example:
Hi world how are ya !asdasdasdasdasd! blue chickens
would become
Hi world how are ya blue chickens
As you can see now there are 2 spaces between ya and blue. I would like these to turn to one space as well.
Try a regex like
note the ” *” at the end to eat up any spaces after it.
Edit: Additionally, you didn’t use the / / delimiters denoting the regex in your example, which is probably why your test didn’t work. I opted for [^!]+ (string not containing ‘!’) instead of .+?, which is an ungreedy match, out of habit. I’m not certain if it’s faster or not, but in this example it probably doesn’t matter.