doutuan8887 2014-01-21 12:18
浏览 72
已采纳

PHP使用preg_replace删除img标记

i can get text from tags between </a></div> and <div class="toolz">. in result of that i have img tag with <br /> after that.

for example:

<img alt="TEXT" title="TEXT"src="IMAGE LINK" /><br />

how to remove from that with preg_replace?

<meta charset='UTF-8' />
<?php
    $handle='http://www.namefa.ir/Names.asp?pn=4&sx=M&fc=%D8%A2';
    preg_match_all('/<\/a><\/div>(.*?)\s*<div[^>]+class="toolz"[^>]*>\s*/si', file_get_contents($handle), $matching_data);
    $default = preg_replace('/<img[^>]*>(.*)\/><br />/is', "", $matching_data);
    echo'<pre>';print_r($default);echo'</pre>';
?>
  • 写回答

1条回答 默认 最新

  • dougu1952 2014-01-21 12:57
    关注

    I think, you code has some logical mistakes.. you have to use an loop to iterate through your search results and replace the tags in your original content. What you do now, seems a little bit useless.

    $handle='http://www.namefa.ir/Names.asp?pn=4&sx=M&fc=%D8%A2';
    $content = file_get_contents($handle);
    preg_match_all('/<\/a><\/div>(.*?)\s*<div[^>]+class="toolz"[^>]*>\s*/si', $content, $matching_data, PREG_SET_ORDER);
    foreach ($matching_data as $match) {
        $replace = preg_replace('/<img[^>]*>(.*)\/><br />/is', "", $match[0]);
        $content = str_replace($matching_data[0], $replace, $content);
    }
    echo'<pre>';print_r($content);echo'</pre>';
    

    It seems your inner regex to replace the image is wrong.. at least it doesn't find something - are you sure, the <br /> is correct, or if you just want to find a simple line break

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证