2012-09-06 5 views
-1

다음 작업을 수행하려면 PHP와 정규 표현식으로 가능할 수 있습니다. 하이퍼 링크의 최상위 도메인이 배열의 주어진 tld 이름과 일치하는 경우 내용 내부의 모든 하이퍼 링크를 가져 와서 다시 작성하십시오.php 정규 표현식 및 바꾸기

지금 주어진 내용에있는 모든 하이퍼 링크를 다시 쓰는 정규 표현식을

preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="goto.php?url=$2"$3>', $content); 

예를

$tld = array("http://www.example.com","http://www.test.com"); 

if <a href="www.example.com">example</a> than <a href="/goto.php?url= www.example.com"</a>; 
+5

대신 DOM 파서를 사용하십시오. – Brad

+3

[그는 조랑말이 온다 ...] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) 오 남자가 점점 –

+0

복잡한 나는 내 믿음을 잃는다. – lgt

답변

1
당신은 당신의 정규식 비트를 공고히 할 수 있습니다

...

$pattern = <<<EOL 
/<a([^>]+)href\s*=\s*(['" ]?)([^"'> ]*)(['" ]?)([^>]*)>/si 
EOL; 

$replacement = "<a$1href='goto.php?url=$3'$5>"; 

preg_replace($pattern, $replacement, $content); 

지금 바로 테스트 할 수 없으므로 오타가있을 수 있습니다.

+0

고마워요 나는 그것과 함께하려고 줄거야 – lgt