Conrad
Conrad

Reputation: 121

preg_replace pattern instead of recursive function for <span style=

I have something like this:

$text = 'Getting the right kitchen appliances and home appliances for your situation is important to us.We want to make sure you are served to the best of our ability. Please feel free to contact us at any time with any inquiries you may have: <h3 style="text-align: left;"><span style="color: #0000ff;"><span style="color: #3366ff;"><span style="color: #0000ff;"><span style="color: #000000;"><strong> </strong>General inquiries (**please include your location**): </span></span></span></span>';

and preg_replace:

preg_replace(
    '/<span style="color(.+?)">(.+?)<\/span>/s', 
    "<span>$2</span>", 
    $text
);

That replaces only first occurrence. How to change that to replace every occurrences of this pattern?

Upvotes: 0

Views: 1089

Answers (2)

zee
zee

Reputation: 1

i think you just want to skip color attribute from text, so why don't you just use color in preg_replace instead of complete span. try this:

preg_replace(
        '/color(.+?);/s', 
         "",  
        $text);

Upvotes: 0

user3470953
user3470953

Reputation: 11335

are you looking for this

$text = 'Getting the right kitchen appliances and home appliances
 for your situation is important to us.We want to make sure you are
 served to the best of our ability. Please feel free to contact us 
at any time with any inquiries you may have: <h3 style="text-align: left;">
<span style="color: #0000ff;"><span style="color: #3366ff;">
<span style="color: #0000ff;"><span style="color: #000000;"><strong>
 </strong>General inquiries (**please include your location**): 
</span></span></span></span>';

$text = preg_replace("/<span[^>]+\>/i", '<span style="\$2">\$3</span>', $text);

Output:

Getting the right kitchen appliances and home appliances for your situation is important to us.We want to make sure you are served to the best of our ability. Please feel free to contact us at any time with any inquiries you may have: <h3 style="text-align: left;"><span style="$2">$3</span><span style="$2">$3</span><span style="$2">$3</span><span style="$2">$3</span><strong> </strong>General inquiries (**please include your location**): </span></span></span></span>

Upvotes: 1

Related Questions