Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap each letter of a string in a tag, avoiding HTML tags

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?

like image 574
Stephanie D. Avatar asked Oct 21 '25 04:10

Stephanie D.


1 Answers

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).

like image 131
Wiktor Stribiżew Avatar answered Oct 23 '25 20:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!