Preg_match_all多个内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Preg_match_all多个内容相关的知识,希望对你有一定的参考价值。

我想从具有preg_match_all功能的站点获取多个数据。但是当我使用这个regex命令(。*?)倍数时,该函数不起作用。

这不是一个json。我从任何instagram个人资料的源代码中获取数据。例如:view-source:https://www.instagram.com/ziynetaki当您打开链接时,您可以看到以下代码。 script type =“text / javascript”> window._sharedData = .....

我试过了:

$display = '#__typename":"GraphImage","id":"(.*?)","edge_media_to_caption":"edges":["node":"text":"(.*?)"],"shortcode":"(.*?)","edge_media_to_comment":"count":(.*?),"comments_disabled":false,"taken_at_timestamp":(.*?),"dimensions":"height":(.*?),"width":(.*?),"display_url":"(.*?)","edge_liked_by"#i';

preg_match_all($display, $url, $dop);
print_r($dop);

并且输出为null:

Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) [4] => Array ( ) [5] => Array ( ) [6] => Array ( ) [7] => Array ( ) )

有人可以帮我这么做吗?我搜索了很多解决方案,但我找不到任何信息。我不知道如何在互联网上搜索,因为我的英语不是很好,谢谢。

答案

您的正则表达式模式有一个小错误,因为您的匹配失败。

你的正则表达式在这部分[中有一个"edges":["node"字符,你没有逃脱,因此它表现为一个字符集并破坏你的正则表达式的预期含义。

只是逃避[让你的模式开始匹配并给出预期的结果。此外,如果您的输入跨越多行,您可以启用DOTALL模式以使.匹配新行,但截至目前,您的输入字符串不需要它并按原样工作。

Check here

检查您的php代码,

$url = '"edges":["node":"__typename":"GraphImage","id":"1998193948597745904","edge_media_to_caption":"edges":["node":"text":"test"],"shortcode":"Bu7As8ygPjw","edge_media_to_comment":"count":0,"comments_disabled":false,"taken_at_timestamp":1552423302,"dimensions":"height":216,"width":320,"display_url":"https://instagram.fdel1-4.fna.fbcdn.net/vp/b26fc82649aebaba80ff8750844d9476/5D32EFAE/t51.2885-15/e35/52508202_381950182383672_2887085630092404003_n.jpg?_nc_ht=instagram.fdel1-4.fna.fbcdn.net","edge_liked_by":"count":6,"edge_media_preview_like":"count":6';
$display = '#__typename":"GraphImage","id":"(.*?)","edge_media_to_caption":"edges":\["node":"text":"(.*?)"],"shortcode":"(.*?)","edge_media_to_comment":"count":(.*?),"comments_disabled":false,"taken_at_timestamp":(.*?),"dimensions":"height":(.*?),"width":(.*?),"display_url":"(.*?)","edge_liked_by"#i';

preg_match_all($display, $url, $dop);
print_r($dop);

打印所有预期的捕获,

Array
(
    [0] => Array
        (
            [0] => __typename":"GraphImage","id":"1998193948597745904","edge_media_to_caption":"edges":["node":"text":"test"],"shortcode":"Bu7As8ygPjw","edge_media_to_comment":"count":0,"comments_disabled":false,"taken_at_timestamp":1552423302,"dimensions":"height":216,"width":320,"display_url":"https://instagram.fdel1-4.fna.fbcdn.net/vp/b26fc82649aebaba80ff8750844d9476/5D32EFAE/t51.2885-15/e35/52508202_381950182383672_2887085630092404003_n.jpg?_nc_ht=instagram.fdel1-4.fna.fbcdn.net","edge_liked_by"
        )

    [1] => Array
        (
            [0] => 1998193948597745904
        )

    [2] => Array
        (
            [0] => test
        )

    [3] => Array
        (
            [0] => Bu7As8ygPjw
        )

    [4] => Array
        (
            [0] => 0
        )

    [5] => Array
        (
            [0] => 1552423302
        )

    [6] => Array
        (
            [0] => 216
        )

    [7] => Array
        (
            [0] => 320
        )

    [8] => Array
        (
            [0] => https://instagram.fdel1-4.fna.fbcdn.net/vp/b26fc82649aebaba80ff8750844d9476/5D32EFAE/t51.2885-15/e35/52508202_381950182383672_2887085630092404003_n.jpg?_nc_ht=instagram.fdel1-4.fna.fbcdn.net
        )

)

Online PHP Demo

以上是关于Preg_match_all多个内容的主要内容,如果未能解决你的问题,请参考以下文章

php 正则表达式 匹配网站内容

PHP 正则表达式匹配 preg_match 与 preg_match_all 函数

多个通配符 preg_match_all php

dedecms 内容页获取图片

php正则抓取网站

2017-07-01