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! =)
take a look at my code...
Moderator: General Moderators
Re: take a look at my code...
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()
But, if you only want to replace some text, you should use php function str_replace()
Re: take a look at my code...
Something like this might work.
becomes
Edit: This post was recovered from search engine cache.
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>jCode: Select all
a<span class="b">c f</span>g<span style="h">i</span>j
Last edited by McInfo on Tue Jun 15, 2010 11:23 pm, edited 1 time in total.
Re: take a look at my code...
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??
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...
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.
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.
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 replacedEdit: This post was recovered from search engine cache.