dqeonr8554
dqeonr8554
2013-07-29 10:52

PHP:使用preg_replace过滤一些html属性。 获得第一个</ span>,而不是最后一个

已采纳

I'm trying to filter an HTML file to remove some attributes. Specifically, I want to remove ALL spans except the ones that set a color. In the spans that set a color I will remove all the attributes except the style='color...'.

I.e., if I have:

<span lang=EN-US>This is a </span>
<span id="myspan" style='color:red;text-align:left;'>test</span>
<span lang=EN-US> to remove spans.</span>

I want it to be:

This is a
<span style='color:red'>test</span>
to remove spans.

To do this I'm using preg_replace. I created this regex:

preg_replace(
    '%(<span [^>]*color\:)([a-z]*)(;|\')([^>]*>)(.*)(<\/span>)%s', 
    "<qwerty style='color:$2'>$5</qwerty>", 
$myText);

After using this, I remove all spans with strip_tags and then I turn all <qwerty> to <span>.

My problem is that the content between <span> and </span> ((.*) in my regex) is getting all the text until the end:

This is a 
<span style='color:red'>test
to remove spans.</span>

I want it to get all the text until it finds the first </span>, but now it gets all the text until the last </span>. How can I do this?

Thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doujiazong0322 doujiazong0322 8年前

    Use ungreedy (U) modifier:

    preg_replace('%....%sU', .....);
    
    点赞 5 评论 复制链接分享