2013-10-30 4 views
1

정규 표현식에서 새로운입니다.정규 표현식과 링크 검색

$text = 
'<ul style="list-style:none;"> 
    <li> 
     <a href="files/docs/qwe.xls" target="_blank">Link1</a> 
    </li> 
    <li> 
     <a href="files/docs/ere.xls" target="_blank">Link2</a> 
    </li> 
    <li> 
     <a href="files/docs/123.xls" target="_blank">Link3</a> 
    </li> 
</ul>'; 

정규 표현식 내가 원하는이 배열을 얻을 :

$filePath[0] = "files/docs/qwe.xls"; 
$fileName[0] = "Link1"; 
$filePath[1] = "files/docs/ere.xls"; 
$fileName[1] = "Link2"; 
$filePath[2] = "files/docs/123.xls"; 
$fileName[2] = "Link3"; 

내가 그것을 어떻게 할 수

나는이 텍스트가?

감사합니다.

+0

것 당신의 정규 표현식을 엉망으로 만드는 경향이 있습니다. 이것 좀 보시기 바랍니다. http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – ffledgling

답변

0

사용

$res = array(); 
preg_match_all('/href="(.+?)".*?>(.+?)<\/a>/ims', $text, $res); 
var_dump($res); 
+0

@ 로버트 : 왜 $ res를 먼저 정의해야합니까? – TiMESPLiNTER

+0

또한 정의되지 않은 배열의 오류를 줄 것이므로 사용하려는 변수를 선언하는 것이 좋습니다. Btw 귀하의 코드는 여러 줄로 실패합니다 – Robert

+0

하지만 $ E_ALL error_reporting()을 활성화해도 $ res는 preg_match_all에 의해 정의되고 PHP 오류를 전혀 발생시키지 않습니다. 다중 행은 ​​요구 사항이 아니 었습니다. – TiMESPLiNTER

0

사용 lookarounds, 그들은 전에 또는 당신이 찾고있는 문자열 뒤에 뭔가가 있는지 확인하는데 유용하다. 여기 그것이 작동하는 방법이다 :

/
(?<=href=")href="
[^"]*"
/ 다음 비 " 문자
(?=")의 nomber 앞에 시작 :

여기
/(?<=href=")[^"]*(?=")/ 

무슨 뜻인지입니다 끝

2

당신은 간단한 정규 표현식을

확인이 코드

$match = array(); 
preg_match_all('#<a href="(.*?)">(.*?)</a>#sm', $text, $match); 
print_r($match) 

(.*?) 필요 - 의미를 비 모든 욕심 이상적으로 한 정규 표현식, 텍스트에 조금이라도 변화에 HTML을 구문 분석하지 않아야

+0

@TiMESPLiNTER did the op write that uppercase is needed? It's just about adding "i" to "#sm" :) – Robert

+0

Yeah I know but you wrote in my answer that my code is not working with multiline and this was nod needed either bei the op an his example. So I thought I'll complete your answer too. – TiMESPLiNTER