代码之家  ›  专栏  ›  技术社区  ›  ntan

获取图像的php regexp[重复]

  •  0
  • ntan  · 技术社区  · 16 年前

    可能重复:
    Regex to change format of all img src attributes

    你好,

    我想替换内容数据库字段中的图像路径。

    我有以下内容

    preg_replace("/src='(?:[^'\/]*\/)*([^']+)'/g","src='newPath/$2'",$content);
    

    哪个工作正常

    src="/path/path/image.jpg"
    

    但失败了

    src="http://www.mydomain.com/path/path/image.jpg"
    

    有什么帮助可以绕过这个问题吗?

    1 回复  |  直到 16 年前
        1
  •  2
  •   Pekka 웃    16 年前

    不要为此使用正则表达式。使用类似HTML语法分析器 Simple HTML DOM .

    $html = file_get_html('http://www.example.com/sourcepage.html');
    
    foreach($html->find('img') as $element)  
     {      
        $new_src = "Do stuff with new src here"; 
        $element->src = $new_src;
     }
    
     echo $html; // Output new code
    
    推荐文章