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 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.