【问题标题】:How to print two or more results using file_get_contents() and preg_match()如何使用 file_get_contents() 和 preg_match() 打印两个或多个结果
【发布时间】:2017-09-16 12:24:37
【问题描述】:

这是我当前的代码:

$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match('|<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="|' , $cast , $castimg );
echo $castimg[1];

使用此代码,我试图获取该页面中的所有 data-src="" 值,但我只知道如何打印第一个,我知道这与数组有关,但我是一种新手编程,所以这是我做不到的。等待您的答复。

【问题讨论】:

    标签: php arrays preg-match file-get-contents


    【解决方案1】:

    要匹配多个实例,请使用preg_match_all

    preg_match_all('|<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="|' , $cast , $castimg );
    

    【讨论】:

    • 好的,那么我如何将它们堆叠在一个变量中或将它们全部打印出来
    • @IsmaelQuirantes:就print_r($castimg);
    • 我之前试过,打印出来的只是“Array ([0] => Array ([0] =>" any data-src="" 我想要的:(
    • @IsmaelQuirantes:为我工作,你确定$cast 包含你想要匹配的内容吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多