Welcome Guest, Not a member yet? Register   Sign In
Regular Expressions adding SPAN tag around words
#1

[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
#2

[eluser]yannyannyann[/eluser]
I have tried this :

Code:
preg_match_all( '/(\b(\w+)\b.*?)/',$text,$matches)
gives me :
Code:
[0] => Array
        (
            [0] => h2
            [1] => class
            [2] => asdas
            [3] => Hello
            [4] => you
            [5] => are
            [6] => 25
            [7] => years
            [8] => old
            [9] => h2
            [10] => p
            [11] => How
            [12] => are
            [13] => b
            [14] => YOU
            [15] => b
            [16] => doing
            [17] => p
        )

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 >
#3

[eluser]richthegeek[/eluser]
Code:
&lt;?php

$in = "<h2 class='test'>Hello</h2><p>Welcome, <b>Joe</b> , how are you?</p>";

$reg = "/(<[^>]+>)?([^< ]+)(<[^>]+>)?/";
$rep = "$1<span>$2</span>$3";

print preg_replace( $reg, $rep, $in );

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>
#4

[eluser]richthegeek[/eluser]
Slightly closer to teh originally requested output code:

Code:
&lt;?php

$in = "<h2 class='test'>Hello</h2><p>Welcome, <b>Joe</b> , how are you?</p>";

$reg = "/(<[^>]+>)?( ?)([^<, ]+)( ?)(<[^>]+>)?/";
$rep = "$1$2<span>$3</span>$4$5";

print preg_replace( $reg, $rep, $in );

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>
#5

[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
#6

[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>
#7

[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:
&lt;?php
$in = "<p><strong>Hello World</strong></p><p>Nice to meet you</p>";
$reg = "/(<[^>]+>)?( ?)([^< ]+[ ]*)( ?)(<[^>]+>)?/";
$rep = "$1$2<span id='w_'>$3</span>$4$5";
$output = preg_replace( $reg, $rep, $in );
?&gt;

Current output(bug on first html tag)
Code:
<<span>p></span><strong><span>Hello</span> <span>World</span></strong></p>
<p><span>Nice</span> <span>to</span> <span>meet</span> <span>you</span></p>


DESIRED OUTPUT:
Code:
<p><strong><span>Hello </span><span>World</span></strong></p>
<p><span>Nice </span><span>to </span><span>meet </span><span>you </span></p>

I would like also to be able to have on the first word the styles:
Code:
<p><em>First</em> Words</p>
or
<h2><em>First</em> Words</h2>


Basically, any combinaison of 2 html tags.

Would this be possible inside the Reg Expr ?

many thanks in advance !!
#8

[eluser]yannyannyann[/eluser]
Anybody to help pls ?
I am trying again, but I am not very efficient with Reg Expr. Sad




Theme © iAndrew 2016 - Forum software by © MyBB