2013-07-25 2 views
1
function makeLinksInTheContent($html) 
{ 
    $html= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" rel=\"nofollow\" >$3</a>", $html); 
    $html= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" rel=\"nofollow\" >$3</a>", $html); 
    $html= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:[email protected]$3\" rel=\"nofollow\">[email protected]$3</a>", $html); 

    return($html); 
} 

이것은 내 코드입니다.preg_replace는 일치하는 URL의 문자열을 대체합니다.

내 URL이 자동으로 필요합니다. preg_replace를 사용하여 url을 찾고이 url에 대한 링크를 설정하십시오.

예 : '페이지에 www.google.com이 있습니다.' 이 콘텐츠를 makeLinksInTheContent ($ html)에 전달하면 "www.google.com이 포함 된 페이지"가 ​​반환됩니다.

하지만 다음 URL 형식은 연결되지 않습니다.

  1. (http://www.google.com)

  2. www.test.com()와 [] &^%의 $의 # @ + | @ # $으로의 %^&() _ +} {! "? > <,./'[] = - 09 ~`.co, in, com.com

  3. http://www.test.com() 및 [] &^% $ # @! + | ! @ # $ %^&() _ +} {: "?> <,. /; [] = - 09 ~`.co, in, com.com

  4. https://www.test.com() 및 [] &^% $ # @! +!! @ # $ %^&() _ +} {: "?> <,. /; '[] = -09 ~`.co와, com.com

  5. ftp://www.test.com()와 [] &^%의 $ 번호에 @ + |! @ # $으로의 %^&() _ +} {:?. "> <, /, '[] = - 09 ~`.co와, com.com

에 내가 생각하는 내 정규 표현식은 몇 가지 실수가 있습니다. 제발 우리를 제안하십시오.

+0

출력 전에...? 이 EXACT 질문 – SmokeyPHP

+0

[preg \ _replace가 일치하는 URL을 대체하는 문자열] 중복 됨 (http://stackoverflow.com/questions/17783918/preg-replace-to-replace-string-for-matching-url) – SmokeyPHP

답변

0

이 물었다 마지막에 내 대답 :

preg_replace to replace string for matching url

내 기능을 사용하는 것이 좋습니다 수 있습니다

- 당신의 데이터와 테스트를 나를

function parse_links($string,$mailto=true) 
{ 
    $preg_r = "#(((https?|ftp)(://)?)?[a-zA-Z0-9-_.]{1,64}\.[a-zA-Z0-9-_.]{1,128}\.[a-z]{2,4}(\.[a-z]{2,4})?(/([^ ]{1,256}))?/?)#"; 
    $string = preg_replace($preg_r,"<a href=\"http://$1\">$1</a>",explode(" ",$string)); 
    if($mailto) 
    { 
     $preg_r2 = "/([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z_\-\.][-\w]*[0-9a-zA-Z_\-\.]\.)+[a-zA-Z]{2,9})/"; 
     $string = preg_replace($preg_r2,"<a href=\"mailto:$1\">$1</a>",$string); 
    } 
    return implode(" ",$string); 
} 

마찬가지로 false를 전달 작동 이메일 링크를 만들지 않으려면 두 번째 매개 변수를 입력하십시오.

+0

다음 URL 연결되지 않습니다. (www.google.com), www.test..co, in, com() 및 [] &^% $ # @! + |! @ # $ %^&() _ +} {: "?" com.com, https://www.test.com() 및 [] &^% $ # @! + |! @ # $ %^&() _ +} {: "?,. /; ' [] = - 09 ~'.co, in, com.com – user2349080

+0

문자열'www.google.com','www.test.com'과'https://www.test.com '을 테스트 할 때 세 개의 링크가 있습니다. '- 당신은 무엇을 잡으려고 했습니까? – SmokeyPHP

+0

http://phpfiddle.org/lite/code/zuv-y3k (오른쪽 상단에서 실행을 누르십시오) – SmokeyPHP

3

이 경우 preg_replace_callback을 사용할 수 있습니다. Read more

기능

<?php 
    function replace_urls($text = null) { 
    $regex = '/((http|ftp|https):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\[email protected]?^=%&amp;\/~+#-])?/'; 
    return preg_replace_callback($regex, function($m) { 
     $link = $name = $m[0]; 
     if (empty($m[1])) { 
     $link = "http://".$link; 
     } 
     return '<a href="'.$link.'" target="_blank" rel="nofollow">'.$name.'</a>'; 
    }, $text); 
    } 
?> 

사용

<?php 
    $text = "http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054 
    www.google.com 
    https://twitter.com/ 
    http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar"; 

    echo replace_urls($text); 
?> 

이미이 질문에 대답했습니다

<a href="http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054" target="_blank" rel="nofollow">http://stackoverflow.com/questions/17854971/preg-replace-to-replace-string-for-matching-url#17855054</a> 
<a href="http://www.google.com" target="_blank" rel="nofollow">www.google.com</a> 
<a href="https://twitter.com/" target="_blank" rel="nofollow">https://twitter.com/</a> 
<a href="http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar" target="_blank" rel="nofollow">http://www.somelinkwithhash.com/post/4454/?foo=bar#foo=bar</a> 
+0

다음 URL은 연결되지 않습니다. – user2349080

관련 문제