user390480
user390480

Reputation: 1665

Remove question marks between these two tags

I have, for example, this string:

asd? asdfasdfsdf <description>&lt;div? style=&quot;color:000000;font-family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;width:555px;&quot;&gt;Bohus Malm?t;/a&gt;</description> blah blah blah? asdfasfize:12px;font-size:?

I need to know how I can remove all the ? that exists only between the opening and closing "description" tags, but not the ones outside it.

Thanks!

Upvotes: 0

Views: 286

Answers (3)

austinbv
austinbv

Reputation: 9491

<?php
$string = 'asd? asdfasdfsdf <description>&lt;div? style=&quot;color:000000;font-family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;width:555px;&quot;&gt;Bohus Malm?t;/a&gt;</description> blah blah blah? asdfasfize:12px;font-size:?';
$regex = '/(<description>.*)\?(.*<\/description>)/i';

while (preg_match($regex, $string)) {
     $string = preg_replace($regex, '$1$2', $string);
}
echo $string; 
?>

Upvotes: 0

Halil &#214;zg&#252;r
Halil &#214;zg&#252;r

Reputation: 15945

$str = 'asd? asdfasdfsdf <description>&lt;div? style=&quot;color:000000;font-' . 
       'family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;' . 
       'width:555px;&quot;&gt;Bohus Malm?t;/a&gt;</description> blah blah ' . 
       'blah? asdfasfize:12px;font-size:?';  
function myReplace($matches)  
{  
    return $matches[1].str_replace('?', '', $matches[2]).$matches[3];  
}  
$result = preg_replace_callback(
    '|(.*<description>)(.*)(<\/description>.*)|', 
    'myReplace', 
    $str
);  
echo htmlspecialchars($result);  

Upvotes: 1

Kalessin
Kalessin

Reputation: 2302

What about:

$var = 'asd? asdfasdfsdf <description>&lt;div? style=&quot;color:000000;font-family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;width:555px;&quot;&gt;Bohus Malm?t;/a&gt;</description> blah blah blah? asdfasfize:12px;font-size:?';
preg_match( '/(.*)(<description>.*<\/description>)(.*)/', $var, $matches );
$new = $matches[1] . str_replace( '?', '', $matches[2] ) . $matches[3];

Upvotes: 0

Related Questions