我想使用preg_match_all函数从一个站点获取多个数据。但是当我多次使用此正则表达式命令(。*?)时,该功能不起作用。
它不是json。我从任何instagram个人资料的源代码中获取数据。例如:view-source:https://www.instagram.com/ziynetaki当您打开链接时,可以看到以下代码。脚本类型=“ 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 ( ) )
有人可以帮我吗?我搜索了很多解决方案,但找不到任何信息。我不知道如何在互联网上进行搜索,因为我的英语不太好,谢谢。
答案 0 :(得分:0)
由于匹配失败,您的正则表达式模式有一个小错误。
您的正则表达式在这部分[
中有一个{"edges":[{"node"
字符,您没有转义,因此它表现为字符集,并且破坏了正则表达式的预期含义。
只需逃避[
,您的模式就可以开始匹配并给出预期的结果。另外,如果您的输入跨越多行,则可以启用DOTALL
模式以使.
匹配新行,但是到目前为止,您的输入字符串并不需要它并且可以按原样工作。
检查您的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
)
)