Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
preg_match显示来自其他php/html文件的第一个图像_Php_Html - Fatal编程技术网

preg_match显示来自其他php/html文件的第一个图像

preg_match显示来自其他php/html文件的第一个图像,php,html,Php,Html,我试图在网站上自动显示我的新博客条目。因此,我找到了一个小php脚本,并根据自己的需要对其进行了修改: if(preg_match(“//i”,$data,$titleb)){$bild=$titleb[1];} 我希望这个脚本基本上显示在我的网站上的最新标题,一个小演习,并显示最新的图像。 标题效果很好,但我无法从图片中得到任何东西。我怎样才能解决这个问题?谢谢大家的帮助。请不要对我太生气,我只是个初学者 (很抱歉,我没有发布整个脚本,因为我无法让StackOverflow接受格式正确的脚本)

我试图在网站上自动显示我的新博客条目。因此,我找到了一个小php脚本,并根据自己的需要对其进行了修改:

if(preg_match(“//i”,$data,$titleb)){$bild=$titleb[1];}

我希望这个脚本基本上显示在我的网站上的最新标题,一个小演习,并显示最新的图像。 标题效果很好,但我无法从图片中得到任何东西。我怎样才能解决这个问题?谢谢大家的帮助。请不要对我太生气,我只是个初学者

(很抱歉,我没有发布整个脚本,因为我无法让StackOverflow接受格式正确的脚本)

检查以下内容:

$html = "<html><body><a href='post.html'><img src='img.jpg'>View post</a> <a href='post2.html'><img src='http://www.yandex.ru/image/img2.jpg'>View post 2</a> <a href='post2.html'><img src='http://www.yandex.ru/image/img3.png'>View post 3</a></body></html>";

echo $html . "\n preg: ";

# get all img-tags
preg_match_all('/<img[^>]+>/i',$html, $result);
$img = array();
foreach( $result[0] as $k => $img_tag){
    echo 'img_tag: ';
    print_r($img_tag);
    echo "\n";
    preg_match_all('/<img[^>]+src="?\'?([^"\']+)"?\'?[^>]*>/i', $img_tag, $img[$k], PREG_SET_ORDER);
}

# show image urls
foreach ($img as $image) {
    echo $image[0][1] . "\n"; # image URLs
}
$html=”“;
echo$html。“\n preg:”;
#获取所有img标签
preg_match_all('/]*>/i',$img_tag,$img[$k],preg_SET_ORDER);
}
#显示图像URL
foreach($img作为$image){
echo$image[0][1]。“\n”#图像URL
}
示例输出:


preg:img_标签:
img_标签:
img_标签:
img.jpg
http://www.yandex.ru/image/img2.jpg
http://www.yandex.ru/image/img3.png
变量$img内容:

数组(3){
[0] =>
阵列(1){
[0] =>
阵列(2){
[0] =>
字符串(19)”
[1] =>
字符串(7)“img.jpg”
}
}
[1] =>
阵列(1){
[0] =>
阵列(2){
[0] =>
字符串(47)”
[1] =>
字符串(35)”http://www.yandex.ru/image/img2.jpg"
}
}
[2] =>
阵列(1){
[0] =>
阵列(2){
[0] =>
字符串(47)”
[1] =>
字符串(35)”http://www.yandex.ru/image/img3.png"
}
}
}
非常感谢您的支持(我唯一需要的是'/]*>/i'部分-现在它工作得非常好。
<html><body><a href='post.html'><img src='img.jpg'>View post</a> <a href='post2.html'><img src='http://www.yandex.ru/image/img2.jpg'>View post 2</a> <a href='post2.html'><img src='http://www.yandex.ru/image/img3.png'>View post 3</a></body></html>
 preg: img_tag: <img src='img.jpg'>
img_tag: <img src='http://www.yandex.ru/image/img2.jpg'>
img_tag: <img src='http://www.yandex.ru/image/img3.png'>
img.jpg
http://www.yandex.ru/image/img2.jpg
http://www.yandex.ru/image/img3.png
array(3) {
  [0] =>
  array(1) {
    [0] =>
    array(2) {
      [0] =>
      string(19) "<img src='img.jpg'>"
      [1] =>
      string(7) "img.jpg"
    }
  }
  [1] =>
  array(1) {
    [0] =>
    array(2) {
      [0] =>
      string(47) "<img src='http://www.yandex.ru/image/img2.jpg'>"
      [1] =>
      string(35) "http://www.yandex.ru/image/img2.jpg"
    }
  }
  [2] =>
  array(1) {
    [0] =>
    array(2) {
      [0] =>
      string(47) "<img src='http://www.yandex.ru/image/img3.png'>"
      [1] =>
      string(35) "http://www.yandex.ru/image/img3.png"
    }
  }
}