0

Ive got a large string with some markup in it I want to change in order for it to work with fpdf.

<span style="text-decoration: underline;">some text</span>

I need to replace the tags here with

<i>some text</i>

However a simple str_replace(); wont work because there are span tags that should not be replaced. I need to make something that finds <span style="text-decoration: underline;">

and then looks for the next occurence of </span> and only replaces that. I haven't got the slightest clue on how to do this. I've looked at http://us.php.net/strpos but not sure on how to implement that, and if that will be the solution. Can anyone give me some pointers?

Thanks.

Snuur
  • 311
  • 1
  • 3
  • 11

2 Answers2

0

This should do the trick:

<?php
    $in = '<span>Invalid</span><span style="text-decoration: underline;">some text</span><span>Invalid</span>';
    $out = preg_replace('@<span style=".*">([^<]+)<\/span>@', '<i>\1</i>', $in);
    echo $out;
?>

View on Codepad.org

You can also restrict what text you'll look for in the tag, for example, only alphanumerics and whitespaces:

<?php
    $in = '<span>Invalid</span><span style="text-decoration: underline;">some text</span><span>Invalid</span>';
    $out = preg_replace('@<span style=".*">([\w|\s]+)<\/span>@', '<i>\1</i>', $in);
    echo $out;
?>

View on Codepad.org

karllindmark
  • 6,031
  • 1
  • 26
  • 41
0
$dom = new domDocument;
$dom->loadHTML($html); 
$spans = $dom->getElementsByTagName('span');
foreach ($spans as $node){
    $text = $node->textContent;
    $node->removeChild($node->firstChild);
    $fragment = $dom->createDocumentFragment();
    $fragment->appendXML('<i>'.$text.'</i>');
    $node->appendChild($fragment);
}
$out = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
Expedito
  • 7,771
  • 5
  • 30
  • 43