Reputation: 635
I am looking for a solution for search and replace. I would need to add an empty space after specific string BUT only if there isn't one already. Regular expressions are to advanced for me. Anyone wants to help me out?
For example
Lorem ipsum[something]abcdefg[/something]dolor sit amet
Needs to be replace with
Lorem ipsum[something]abcdefg[/something] dolor sit amet
So, I need to search for [/something]
and if it is not followed by space, then do the replace...
Upvotes: 2
Views: 76
Reputation: 22158
The regular expression can be this:
/(\[\/something\])(?!\s)/gmi
You can test it and know the whole explanation here:
https://regex101.com/r/pC4pQ3/1
If you see the first is targetted because is not an space, but the second is the opposite.
To apply this in PHP you must to use preg_replace()
. Something like this:
$string = "Lorem ipsum[something]abcdefg[/something]dolor sit amet";
echo preg_replace("/(\[\/something\])(?!\s)/", "\1 ", $string);
// result: "Lorem ipsum[something]abcdefg[/something] dolor sit amet"
Upvotes: 2