replacing all image src tags in HTML text

只谈情不闲聊 提交于 2019-12-09 08:33:29

Use a HTML DOM parser instead, much easier to use and maintain http://simplehtmldom.sourceforge.net/

You should look into preg_replace_callback(), which will allow you to postprocess each match however you like, using a callback function. (You would use it instead of your preg_match_all(), not in addition to it.)

theotherlight

I asked a question yesterday about a good interface for modifying and traversing HTML files. You may be interested in this:

jQuery port to PHP

This may be a good alternative if you are already familiar with jQuery's API.

A non-validating parser may be even better if you need to work with badly formed HTML.

http://pear.php.net/package/XML_HTMLSax3

I think the easiest answer you're looking for is to do a str_replace.

foreach ($images as $img) {
    ..my changes here..
    $body = str_replace($original_string, $modified_string, $output_body);
}

Don't what you want is to use preg_replace? With the e modifier the replacement text is eval'd so you can have a function that do on the text-to-be-replaced the same thing that you would have done in your foreach loop.

EDIT: preg_replace_callback is cleaner than using the e modifier with preg_replace, didn't thought of that while writing my anser, so chaos answer is better.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!