get image url from array

不羁岁月 提交于 2021-01-29 05:31:03

问题


I want to get image url from array.

    $img_link = "http://testserver1.com:8080/test%20messages%20and%20so%20on<br /><br /><div style="text-align: center"><img width="396" height="342" src="http://testserver2.com/photos/5186145181.jpg" alt="" style="border: medium none" /></div><br />test messages and so on <br /><br />"

I want to grab image url "http://testserver2.com/photos/5186145181.jpg", and put this into "img_link_results".

$img_linkA = explode(' ', $img_link); 
$img_link_results = array(); 
foreach($img_linkA as $img_link) { 
    if(preg_match_all('/<img[^>]+>/i', trim($img_link))) { 
        $img_link_results[] = trim($img_link); 
        if (preg_match('#^http:\/\/(.*)\.(gif|png|jpg)$#i', $img_link_results, $tmp)){ 
            $img_link_results = $tmp; 
        }           
    } 
} 
//show results 
echo "<img src='$img_link_results' width='100px' height='100px'>".'<br />';
foreach($img_link_results as $val){ 
    echo "<img src='$img_link_results' width='100px' height='100px'>".'<br />';
}

However, the the result at web page is "Array".

Please let me know what's wrong.

Thanks in advance.


回答1:


You are echoing $img_link_results which is an array. You need to use $val, the array element as follows:

//show results 
foreach($img_link_results as $val){ 
    echo "<img src='$val' width='100px' height='100px'>".'<br />';
}



回答2:


Instead of using

echo "<img src='$img_link_results' width='100px' height='100px'>".'<br />';

try using $valinstead

echo "<img src='$val' width='100px' height='100px'>".'<br />';



回答3:


Add in var_dump($img_link_results); and you'll see that it's empty.

//output
array(0) {
}

Something is broken further up.

Demo here: http://codepad.org/gHKtBbtd



来源:https://stackoverflow.com/questions/9427694/get-image-url-from-array

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