Reputation: 1665
I have, for example, this string:
asd? asdfasdfsdf <description><div? style="color:000000;font-family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;width:555px;">Bohus Malm?t;/a></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
Reputation: 9491
<?php
$string = 'asd? asdfasdfsdf <description><div? style="color:000000;font-family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;width:555px;">Bohus Malm?t;/a></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
Reputation: 15945
$str = 'asd? asdfasdfsdf <description><div? style="color:000000;font-' .
'family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;' .
'width:555px;">Bohus Malm?t;/a></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
Reputation: 2302
What about:
$var = 'asd? asdfasdfsdf <description><div? style="color:000000;font-family:Arial, Helvetica, ?sans-serif;font-size:12px;font-size:12px;width:555px;">Bohus Malm?t;/a></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