dongling2038 2015-02-18 07:07
浏览 34
已采纳

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

I've an associative array titled $data as follows:

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" />
)
)

The actual array is very huge, here I've put in only the necessary data from array. Now what I want is for every element of the array the key [student_image] I should get only the URL of the image, no imag tag and any other data. In short I want following output of above array:

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
)
)

How should I achieve this in best possible and optimum way for all the elements of an array $data?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duanrenchuo9244 2015-02-18 07:22
    关注

    try something like this:

       //$data is your array
        $data = array_map(function($elem){
           //$elem is each elemet  of you array
           return array('student_image' => preg_replace('/<img\ssrc="([^"]+)".+$/','$1',$elem['student_image']));
        },$data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置