2009-07-04 5 views
8

I've created this regexhttp : // 또는 www를 <a href.. in PHP

(www|http://)[^ ]+ 

that match every http://... or www.... but I dont know how to make preg_replace that would work, I've tried

preg_replace('/((www|http://)[^ ]+)/', '<a href="\1">\1</a>', $str); 

but it doesn't work, the result is empty string.

+0

Dup는 : http://stackoverflow.com/ questions/507436/how-do-i-linkify-urls-in-a-string-with-php –

답변

14

You need to escape the slashes in the regex because you are using slashes as the delimiter. You could also use another symbol as the delimiter.

// escaped 
preg_replace('/((www|http:\/\/)[^ ]+)/', '<a href="\1">\1</a>', $str); 

// another delimiter, '@' 
preg_replace('@((www|http://)[^ ]+)@', '<a href="\1">\1</a>', $str); 
+2

축하해! 혼란에서 잘못된 정규식을 복사하지 않는 사람은 당신뿐입니다. – Gumbo

+1

http : // www 앞에 www가 자동으로 추가되지 않으므로 완전히 맞지 않습니다. www.google.com은 ... instead of

1
preg_replace('!((?:www|http://)[^ ]+)!', '<a href="\1">\1</a>', $str); 

When you use / as your pattern delimiter, having / inside your pattern will not work out well. I solved this by using ! as the pattern delimiter, but you could escape your slashes with backslashes instead.

I also didn't see any reason why you were doing two paren captures, so I removed one of them.

Part of the trouble in your situation is that you're running with warnings suppressed; if you had error_reporting(E_ALL) on, you'd have seen the messages PHP is trying to generate about your delimiter problem in your regex.

+0

가장 많이 사용 된 재미있는 표현은 잘못된 정규 표현식을 사용했습니다. – Gumbo

+0

나는 우리가 모두 OP를 복사하고 있다고 상상한다. 어떤 경우에도 수정되었습니다. – chaos

1

Your main problem seems to be that you are putting everything in parentheses, so it doesn't know what "\1" is. Also, you need to escape the "/". So try this:

preg_replace('/(www|http:\/\/[^ ]+)/', '<a href="\1">\1</a>', $str);

Edit: It actually seems the parentheses were not an issue, I misread it. The escaping was still an issue as others also pointed out. Either solution should work.

+0

저는 앞뒤 참조에 여는 괄호로 번호를 매기는 것을 추가하고 싶습니다. 예를 들어, 정규식 "T (e (st))"에서 \ 1은 "est"를 포함하고 \ 2는 "st"를 포함합니다. –

2

When using the regex codes provided by the other users, be sure to add the "i" flag to enable case-insensitivity, so it'll work with both HTTP:// and http://. For example, using chaos's code:

preg_replace('!(www|http://[^ ]+)!i', '<a href="\1">\1</a>', $str); 
2

First of all, you need to escape—or even better, replace—the delimeters as explained in the other answers.

preg_replace('~((www|http://)[^ ]+)~', '<a href="\1">\1</a>', $str); 

Secondly, to further improve the regex, the $n replacement reference syntax is preferred over \\n, as stated in the manual으로 바꾸는 방법.

preg_replace('~((www|http://)[^ ]+)~', '<a href="$1">$1</a>', $str); 

셋째, 불필요한 캡처 괄호를 사용하면 작업 속도가 느려집니다. 그들을 제거. $1$0으로 업데이트하는 것을 잊지 마십시오. 궁금한 점이 있으시면 (?:)을 포착하지 않는 괄호입니다.

preg_replace('~(?:www|http://)[^ ]+~', '<a href="$0">$0</a>', $str); 

마지막으로, 나는 \s의 반대 짧은하고 정확한 \S[^ ]+을 대체 할 것이다. [^ ]+은 공백을 허용하지 않지만 개행과 탭을 허용합니다. \S하지 않습니다. 문자열 대신 공간의 줄 바꿈으로 구분에 포함 된 여러 URL이있는 경우

preg_replace('~(?:www|http://)\S+~', '<a href="$0">$0</a>', $str); 
1

, 당신의 \ S를 사용할 필요가

preg_replace('/((www|http:\/\/)\S+)/', '<a href="$1">$1</a>', $val);