如何检查PreG_Match中是否复杂文本



我已经看到了一些有关它的帖子,但是我的文字有点复杂,

我无法使它工作。

我页面的一部分:

otherurl":"http://cdn1-test.peer5.net:80/edge/71-1.stream/playlist.m3u8?uid=35577u0026sil=3u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3Du0026sid=151078248u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}}

我尝试的是:

preg_match("/otherurl":"http://cdn1-test.peer5.net:80/edge/71-1.stream/playlist.m3u8?uid=(.*)/", $data[$n], $output);
echo $output[1];

我想提出的内容:

只是uid =*

之后的数字

如果您收到的字符串的格式可靠地格式化,其中 uid=参数是?之后的第一个查询参数,并且严格来说是数字字符串,您 can can preg_match()(d+)(匹配数字)匹配来提取它,因为下一个查询参数中的任何内容都不会以数字开头。

$str = 'otherurl":"http://cdn1-test.peer5.net:80/edge/71-1.stream/playlist.m3u8?uid=35577u0026sil=3u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3Du0026sid=151078248u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}}';
preg_match('/?uid=(d+)/', $str, $output);
echo $output[1];
// Prints "35577"

实际上我会避免这种情况。处理此操作的最佳方法是将其视为JSON流,结合PHP的内置URL处理方法parse_url()parse_str()

该解决方案看起来像:

// Note: I made this segment a valid JSON string...
$input_json = '{"otherurl":"http://cdn1-test.peer5.net:80/edge/71-1.stream/playlist.m3u8?uid=35577u0026sil=3u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3Du0026sid=151078248u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}';
$decoded = json_decode($input_json, TRUE);
// Parse the URL and extract its query string
// PHP_URL_QUERY instructs it to get only the query string
// but if you ever need other segments that can be removed
$query = parse_url($decoded['otherurl'], PHP_URL_QUERY);
// Parse out the query string into array $parsed_params
$params = parse_str($query, $parsed_params);
// Get your uid.
echo $parsed_params['uid'];
// Prints 35577

最新更新