경우] : 나는 제시 할 무엇
preg_match("/otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=(.*)/", $data[$n], $output);
echo $output[1];
받은 문자열은 신뢰할 수있는 형식으로 게시 된 예제처럼 표시됩니다. 여기서 uid=
매개 변수 i 첫 번째 쿼리 매개 변수는 ?
다음에 엄격하게 숫자 문자열입니다. (\d+)
(일치하는 숫자)과 일치시켜이를 추출 할 수 있습니다. 다음 쿼리 매개 변수 다음에 오는 숫자는 숫자로 시작하지 않기 때문입니다.
$str = 'otherurl":"http:\/\/cdn1-test.peer5.net:80\/edge\/71-1.stream\/playlist.m3u8?uid=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=4OFxyLUs7UrIeWujPzuU%3D"}}';
preg_match('/\?uid=(\d+)/', $str, $output);
echo $output[1];
// Prints "35577"
실제로 나는 이것을 피할 것입니다. 이를 처리하는 가장 좋은 방법은 PHP의 내장 URL 처리 메소드 parse_url()
및 parse_str()
과 함께 JSON 스트림으로 처리하는 것입니다.
솔루션처럼 보이는 :
// 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=35577\u0026sil=3\u0026sip=WyIxODUuMTgueC54IiwiMjEwLj4LngiLCI54LngLjE1OC5giXQ%3D%3D\u0026sid=151078248\u0026misc=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
이 문자열의 원인은 무엇입니까? 'json_decode()'이후에'preg_match '를 시도하지 않고'parse_url()'과'parse_str()'을 대신하여 JSON이 될 수있는 것처럼 보입니다. –
@MichaelBerkowski 예, json입니다.하지만 json 디코드가 아닌 preg match로만 작업 할 수 있는지 알고 싶습니다. – ben
'uid'는 항상 숫자 문자열입니까? –