Home > Article > Backend Development > php custom function to get image path from content
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; } ?> |