仅从字符串中回显图像的 src 属性值的函数


Function for echo only the image's src attributum's value from a string

>我想创建一个函数,它只回显字符串中的图像链接。

我的代码失败了,但我不知道为什么。

function csak_a_kepek($bejovo_szoveg){
    $re = '/(?<=src=")(?:.*?)(?=")/ui';
    $eredmeny = preg_match('/(<img[^>]+>)/i', $bejovo_szoveg, $matches);
    for($i=0;$i<count($matches);$i++){
        $nMatches = preg_match($re, $matches[$i], $aMatches);
    }
    for($i=0;$i<count($aMatches);$i++){
        $return .= $aMatches[$i].'/;/';
    }
    return $return;
}

这/;/是一个分隔符,以便更容易分解它。

你能帮我吗,我必须在哪里改变它,让它工作?

工作代码:(感谢红人80)

include('simplehtmldom/simple_html_dom.php');
function only_pictures($string){
    $html = str_get_html($string);
    foreach($html->find('img') as $element) {
      $return .= $element->src . '<br />';
    }
    return $return;
}

你的模式似乎是错误的。试试这个:

$html = '<img class="" src="http://www.google.com" />';
$pattern = '/<img([^>]*)>/i';
preg_match($pattern, $html, $m);
if(!empty($m)) {
    $max = count($m);
    for($i=1; $i<$max; $i++) {
        $pattern = '/src="([^"]*)"/i';
        preg_match($pattern, $m[$i], $m2);
        echo $m2[1]."'n";
    }
}