【发布时间】:2026-01-02 14:00:01
【问题描述】:
我有一个内容,就像
Test test test <img src="abc.png" alt='1'> test test <img src="123.png" alt='2'> test test
我想改成
Test test test <id>1</id> test test <id>2</id> test test
我试过了:
preg_match_all('/<img(.*?)alt=\"(.*?)\"(.*?)>/si', $content, $result, PREG_SET_ORDER);
foreach($result as $val) {
$content = preg_replace('/<img(.*?)alt=\"(.*?)\"(.*?)>/si', '<id>'.$val[2].'</id>', $content);
但它给了我:
Test test test <id>2</id> test test <id>2</id> test test
【问题讨论】:
-
到目前为止你有没有尝试过? * 不是免费的代码编写服务,希望您能try to solve your own problem first。请更新您的问题以显示您已经尝试过的内容,并在minimal, complete, and verifiable example 中展示您面临的具体问题。如需更多信息,请参阅how to ask good questions,并拨打tour of the site :)
-
循环遍历
$result,利用preg_replace的第四个参数限制为1个替换
标签: php regex preg-replace preg-match-all