2017-12-23 11 views
0

나는 그것에 대해 몇 가지 게시물을 이미 보았지만 텍스트는 약간 복잡합니다.preg_match에서 복잡한 텍스트를 확인하는 방법은 무엇입니까?

그리고 제대로 작동하지 않습니다. 내 페이지의

부 :

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"}} 

내가 뭘하려 :

그냥 수를 UID 후 = *

+0

이 문자열의 원인은 무엇입니까? 'json_decode()'이후에'preg_match '를 시도하지 않고'parse_url()'과'parse_str()'을 대신하여 JSON이 될 수있는 것처럼 보입니다. –

+0

@MichaelBerkowski 예, json입니다.하지만 json 디코드가 아닌 preg match로만 작업 할 수 있는지 알고 싶습니다. – ben

+0

'uid'는 항상 숫자 문자열입니까? –

답변

1

경우] : 나는 제시 할 무엇

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 
+0

시도해 보았습니다. 덕분에 많은 도움이되었습니다. (\ d +)가 숫자와 일치하면 일치하는 문자는 무엇입니까? 나중에 쓰길 원할 경우 – ben

+1

글자를 맞추기 위해'([az] +)'범위와 '/ i' 대소 문자를 구분하는 플래그 또는'([A-Za-z] +)'위/아래 범위. '\ d' 단축은'([0-9] +)'로도 표현 될 수 있습니다. –