<?php
$link = mysqli_connect('127.0.0.1','root','root'); //注意,主機域名務必寫為127.0.0.1如果是localhost肯定會出幺蛾子!!!
if ($link)
{
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,"test");
$sql = "select * from posts where id between 293 and 318";
$res = mysqli_query($link,$sql);
while($rows = mysqli_fetch_assoc($res)){
$soContent = $rows['post_content'];
preg_match_all("/<img.*\>/isU",$soContent,$ereg);//正則表達式把圖片的整個都獲取出來了
$img=$ereg[0][0];//圖片
$p="#src=('|\")(.*)('|\")#isU";//正則表達式
preg_match_all ($p, $img, $img1);
$img_path =$img1[2][0];//獲取第一張圖片路徑
if(!$img_path){
echo "ID:".$rows['ID']."無圖片";
$img_path="images/nopic.jpg"; //如果新聞中不存在圖片,用默認的nopic.jpg替換 */
}else{
echo "ID:".$rows['ID']."的圖片地址:".$img_path;
}
echo "<br/>";
}
};
?>
image.png