I'd like to build a function that takes a string and wraps each of its letters in a <span>, except spaces and HTML tags (in my case, <br> tags).
So:
"Hi <br> there."
... should become
"<span>H</span><span>i</span> <br> <span>t</span><span>h</span><span>e</span><span>r</span><span>e</span><span>.</span>"
I had no luck coming up with my own solution so I looked around and I found it surprisingly hard to find exactly what I was looking for.
The closest I found was Neverever's answer here.
However, it didn't seem to work that well, as each character of the <br> tags were wrapped in a <span> and it didn't match accentuated characters such as éèàï.
How should I proceed with this? And why does parsing HTML tags with regex seem so wrong?
You can achieve the result with a ([^\s>])(?!(?:[^<>]*)?>) regex. To enable Unicode support, just use it with u option:
<?php
$re = "/([^\\s>])(?!(?:[^<>]*)?>)/u";
$str = "Hi <br> there.";
$subst = "<span>$1</span>";
$result = preg_replace($re, $subst, $str);
echo $result;
?>
Here you can find the regex explanation and demo.
See sample program without Unicode support and here is one with Unicode support (the difference is in u option).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With