drhdjp97757 2014-09-04 15:48
浏览 26
已采纳

PHP查找并替换某些字符串模式

I have piece of html code in php $string . The problem is, I need to remove all "img" and "a href" tags from it. I guess I should use preg_replace function, but how should my patterns look like?

I would like to replace:

"<img some params and address>" with ""

and

"<a href="some random address with unknown length">my text</a>" with "my text"
  • 写回答

1条回答 默认 最新

  • doulong1987 2014-09-04 15:50
    关注

    You can use preg_replace with Regular Expressions. Something like this:

    $text = preg_replace("/<img (.*?)>/i", "", $text);
    $text = preg_replace("/<a (.*?)>(.*?)</a>/i", "$2", $text);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?