Reputation: 20223
I have a string:
$test = "Test string<i> hello world</i>."
Then I am running
$test = preg_replace('/(<i>{1}\s*)([\w*\d*\D*\W*\x*\O*\S*\s*]*?)(<\/i>{1})/', '<italic>$2</italic>', $test);
And the result is
Test string<italic>hello world</italic>.
Why is the whitespace before the hello world lost?
Here is an example http://pastebin.com/SXFhsCGK.
Thank you.
Upvotes: 0
Views: 66
Reputation: 9644
Solution
As @Reeno puts it in the comments, replacing the <i>
tags by <italic>
ones directly is the way to go (assuming they're all tags you want to replace):
preg_replace('%<(/?)i>%', '<$1italic>', $test)
What was wrong with your regex
The space is lost because it is matched by the \s*
in <i>{1}\s*
, so it's not in the capturing group.
Also, writing [\w\W...]
means "match any character that is either alphanumeric OR any character that is not alphanumeric"... So basically, match everything.
And the {1}
quantifier is (always?) useless (>{1}
is equivalent to >
).
Heuristically, what you wanted to do was use this regex (s
is so that the dot matches newline too):
~<i>(.*?)</i>~s
Upvotes: 2