Page 1 of 1

take a look at my code...

Posted: Wed Jun 17, 2009 2:41 am
by revbackup
I have a variable with a value like this:

$var='<span class="google-src-text" style="direction: ltr; text-align: left">
Jeśli otrzymałeś token ze strony Joobi możesz go wprowadzić klikając na

<span style="font-weight: bold;">Wprowadź Token</span></span>
Si vous avez reçu le jeton Joobi vous pouvez entrer en cliquant sur le

<span style="font-weight: bold;">jeton de clé</span>
</span>';

Now, I want to remove<span style="font-weight: bold;">Wprowadź Token</span>and I have
a code for that:

$var= preg_replace('#<span style\b[^>]*>(?:.*?)>#s','',$var);

but my problem is I dont want to remove <span style="font-weight: bold;">jeton de clé</span>
because when I do the preg_replace, it also removes the <span style="font-weight: bold;">jeton de clé</span>.
I need to remove the <span style="font-weight: bold;">Wprowadź Token</span> when it is inside a <span class>...</span> tag. Like this:

<span class="google-src-text" style="direction: ltr; text-align: left">
Jeśli otrzymałeś token ze strony Joobi możesz go wprowadzić klikając na

<span style="font-weight: bold;">Wprowadź Token</span></span>

so I should have a new value for my variable and it should be like this:

$var = '<span class="google-src-text" style="direction: ltr; text-align: left">
Jeśli otrzymałeś token ze strony Joobi możesz go wprowadzić klikając na
</span>
Si vous avez reçu le jeton Joobi vous pouvez entrer en cliquant sur le
<span style="font-weight: bold;">jeton de clé</span>
</span>';


Notice that the <span style="font-weight: bold;">Wprowadź Token</span> is removed....

How can I make it possible that I only remove the <span style>...</span> when it is inside a <span class>...</span>. If you could see it, the <span style> I removed is the <span style> that is inside the <span class>...</span>.

Please help me with this... THanks in advance! =)

Re: take a look at my code...

Posted: Wed Jun 17, 2009 6:14 am
by alfmarius
It would be easier to read your example, if you could make a dummy example, this code made me dissy..
But, if you only want to replace some text, you should use php function str_replace()

Re: take a look at my code...

Posted: Wed Jun 17, 2009 12:21 pm
by McInfo
Something like this might work.

Code: Select all

<?php
header('Content-Type: text/plain');
 
// A sample string
$str = 'a<span class="b">c<span style="d">e</span>
f</span>g<span style="h">i</span>j';
 
// Replaces vertical whitespace characters with single spaces so the pattern will work
$new_str = preg_replace('/\v+/', ' ', $str);
 
// Pattern to find a single style span within a class span
$pattern = '#(<span\s+class\s*=[^>]+>)(.*?)(<span\s+style\s*=[^>]+>(.*?)</span>)(.*?)(</span>)#';
 
// Reconstructs the string without the style span
$new_str = preg_replace($pattern, '\1\2\5\6', $new_str);
 
echo "$str\n\n$new_str";
?>

Code: Select all

a<span class="b">c<span style="d">e</span>
f</span>g<span style="h">i</span>j
becomes

Code: Select all

a<span class="b">c f</span>g<span style="h">i</span>j
Edit: This post was recovered from search engine cache.

Re: take a look at my code...

Posted: Thu Jun 18, 2009 3:16 am
by revbackup
to McInfo:

how about remove all <span style>...</span> tags inside a <span class>...</span> tag???
In the code you gave me, it will only remove a single <span style>...</span> tag.... how about remove
2 or 3 <span class>...</span> tags??? Is it possible??

Re: take a look at my code...

Posted: Thu Jun 18, 2009 10:26 am
by McInfo
You can use a do-while loop. This will cause preg_replace() to be called N + 1 times where N is the maximum number of style spans within a class span.

Code: Select all

do {
    // Temporary storage
    $old_str = $new_str;
   
    // Reconstructs the string without the style span
    $new_str = preg_replace($pattern, '\1\2\5\6', $old_str);
   
} while ($new_str != $old_str); // Stops looping when nothing was replaced
Maybe someone who knows regular expressions better than I do can come up with something better.

Edit: This post was recovered from search engine cache.