如何在以下场景中仅从整个 <img> 标签中获取图像 URL?

How to get the image URL only from the whole <img> tag in following scenario?

我有一个名为 $data 的关联数组,如下所示:

Array
(
    [0] => Array
        (
[student_image] => <img src="http://34.144.40.142/file/pic/photo/2015/02/02ff1a23db112db834b8f41748242bcb_240.png"  alt=""  width="180"  height="160"  class="photo_holder" />
)

    [1] => Array
        (
[student_image] => <img src="http://34.144.40.142/theme/frontend/foxplus/style/default/image/document/docx.png"  alt="" />
)
[2] => Array
        (
 [student_image] => <img src="http://34.144.40.142/file/pic/photo/2015/02/da46580276da5c3a31b75e8b31d35ddf_240.png"  alt=""  width="180"  height="160"  class="photo_holder" />
)
)

实际的数组很大,这里我只放了数组中需要的数据。 现在我想要的是数组的每个元素的键 [student_image] 我应该只得到图像的 URL,没有图像标签和任何其他数据。简而言之,我想要以下数组的输出:

Array
(
    [0] => Array
        (
[student_image] => http://34.144.40.142/file/pic/photo/2015/02/02ff1a23db112db834b8f41748242bcb_240.png
)

    [1] => Array
        (
[student_image] => http://34.144.40.142/theme/frontend/foxplus/style/default/image/document/docx.png"
)
[2] => Array
        (
 [student_image] => http://34.144.40.142/file/pic/photo/2015/02/da46580276da5c3a31b75e8b31d35ddf_240.png
)
)

对于数组的所有元素,我应该如何以最好和最佳的方式实现这一点$data

提前致谢。

您可以使用正则表达式提取网址

// $data is your existing array with img tags
$urls = array();
foreach ($data as $key => $value){
    foreach ($value as $key1 => $img) {
        preg_match('/<img[^>]*src="([^"]*)"[^>]*\/>/', $img, $srcmatch);
        $urls[$key][$key1] = $srcmatch[1];
    }
}
// now $urls is having only image url and have same structure of $data,
// You may replace $urls with $data
var_dump($urls);

希望对您有所帮助。

尝试这样的事情:

   //$data is your array
    $data = array_map(function($elem){
       //$elem is each elemet  of you array
       return array('student_image' => preg_replace('/<img\ssrc="([^"]+)".+$/','',$elem['student_image']));
    },$data);