Home  >  Article  >  Backend Development  >  php custom function to get image path from content

php custom function to get image path from content

WBOY
WBOYOriginal
2016-07-25 08:59:591283browse
Call up a large amount of data from the database to obtain the path of the first picture in the content. This article provides you with a simple custom function. Friends in need can refer to it.

The code is as follows:

<?php
/**
 * 内容中取图片路径
 * by http://bbs.it-home.org
*/
function s_img($str){
    preg_match_all("/<img .*\ alt="php custom function to get image path from content" >/isU",$str,$ereg);//把图片的<img整个都获取出来了
    $img=$ereg[0][0];//图片
    $p="#src=('|\")(.*)('|\")#isU";
    preg_match_all ($p, $img, $img1);
    $img_path =$img1[2][0];//获取第一张图片路径
    return $img_path;
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn