正则表达式:匹配或替换它

时间:2011-07-22 17:11:51

标签: php regex preg-replace preg-match

下面的正则表达式有什么问题?

$content = '
<span style="text-decoration: underline;">cultural</span> 
<span style="text-decoration: line-through;">heart</span>
<span style="font-family: " lang="EN-US">May</span>
';

$regex = '/<span style=\"text\-decoration\: underline\;\">(.*?)<\/span>/is';
if (!preg_match($regex,$content))
{

    $content = preg_replace("/<span.*?\>(.*?)<\/span>/is", "$1", $content);
}

我想要做的是删除除了跨度之外的所有跨度,

style="text-decoration: underline;

style="text-decoration: line-through;

我该如何解决?

1 个答案:

答案 0 :(得分:1)

DOM方法:

<?php
  $content = '<span style="text-decoration: underline;">cultural</span>'
           . '<span style="text-decoration: line-through;">heart</span>'
           . '<span style="font-family: " lang="EN-US">May</span>';

  $dom = new DOMDocument();
  $dom->loadHTML($content);

  // grab every span, then iterate over it. Because we may be removing
  // spans, we reference the ->length property off the DOMNode and use an
  // iterator ($s)
  $spans = $dom->getElementsByTagName('span');
  for ($s = 0; $s < $spans->length; $s++)
  {
    // grab the current span element
    $span = $spans->item($s);

    // iterate over the attributes looking for style tags
    $attributes = $span->attributes->length;
    for ($a = 0; $a < $attributes; $a++)
    {
      // grab the attribute, check if it's a style tag.
      // if is is, also check if it has the text-decoration applied
      $attribute = $span->attributes->item($a);
      if ($attribute->name == 'style' && !preg_match('/text-decoration:\s*(?:underline|line-through);/i', $attribute->value))
      {
        // found it. Now, referencing its parent, we want to move all the children
        // up the tree, then remove the span itself from the tree.
        $parent = $span->parentNode;
        foreach ($span->childNodes as $child)
        {
          $parent->insertBefore($child->cloneNode(), $span);
        }
        $parent->removeChild($span);

        // decrement the iterator so we don't end up skipping over nodes
        $s--;
      }
    }
  }
相关问题