Preg_match_all 多个内容

Preg_match_all Multiple Content

我想从具有 preg_match_all 功能的站点获取多个数据。但是当我使用这个正则表达式命令 (. *?) multiple.

时,该功能不起作用

这不是 json。我从任何 Instagram 配置文件的源代码中获取数据。 ex: view-source:https: //www.instagram.com/ziynetaki 当你打开 link 你可以看到下面的代码。脚本类型="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);

并且输出为空:

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