![]() |
Regular Expressions adding SPAN tag around words - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Regular Expressions adding SPAN tag around words (/showthread.php?tid=33361) |
Regular Expressions adding SPAN tag around words - El Forum - 08-24-2010 [eluser]yannyannyann[/eluser] Hi there, I'd like to automatically add span tags around each word of a paragraph that already contains HTML code. I tried regular expressions, but I am not that good in that. Could you give a hint of how to start with ? Code: <h2 class='test'>Hello</h2><p>Welcome, <b>Joe</b> , how are you?</p> Final code i'd like to have : Code: <h2 class='test'><span>Hello</span></h2><p><span>Welcome</span> <b><span>Joe</span></b> , <span>how</span> <span>are</span> <span>you</span></p> Thanks in advance Regular Expressions adding SPAN tag around words - El Forum - 08-24-2010 [eluser]yannyannyann[/eluser] I have tried this : Code: preg_match_all( '/(\b(\w+)\b.*?)/',$text,$matches) Code: [0] => Array I wanna add that please don't select html tags. But I dont know how to say NOT to take the words contained in between < and > Regular Expressions adding SPAN tag around words - El Forum - 08-24-2010 [eluser]richthegeek[/eluser] Code: <?php Output: Code: <h2 class='test'><span>Hello</span></h2><p><span>Welcome,</span> <b><span>Joe</span></b> <span>,</span> <span>how</span> <span>are</span> <span>you?</span></p> Regular Expressions adding SPAN tag around words - El Forum - 08-24-2010 [eluser]richthegeek[/eluser] Slightly closer to teh originally requested output code: Code: <?php Output: Code: <h2 class='test'><span>Hello</span></h2><p><span>Welcome</span>, <b><span>Joe</span></b> , <span>how</span> <span>are</span> <span>you?</span></p> Regular Expressions adding SPAN tag around words - El Forum - 08-24-2010 [eluser]yannyannyann[/eluser] Thanks a lot man !!! Waw, quite incredible ;-) I was there http://www.gskinner.com/RegExr/ for a couple of hours today, without success Regular Expressions adding SPAN tag around words - El Forum - 08-24-2010 [eluser]yannyannyann[/eluser] Is there a way I can add an ID to these SPANS ? like iterating an index ? Code: <span id="w0">Hello</span><span id="w1">Welcome</span> Regular Expressions adding SPAN tag around words - El Forum - 09-16-2010 [eluser]yannyannyann[/eluser] I have one more question about this regular expression: is it possible to have in the first paragraph some bold (or any other standard html) ? Code: <?php Current output(bug on first html tag) Code: <<span>p></span><strong><span>Hello</span> <span>World</span></strong></p> DESIRED OUTPUT: Code: <p><strong><span>Hello </span><span>World</span></strong></p> I would like also to be able to have on the first word the styles: Code: <p><em>First</em> Words</p> Basically, any combinaison of 2 html tags. Would this be possible inside the Reg Expr ? many thanks in advance !! Regular Expressions adding SPAN tag around words - El Forum - 09-22-2010 [eluser]yannyannyann[/eluser] Anybody to help pls ? I am trying again, but I am not very efficient with Reg Expr. ![]() |