2009-07-04 9 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の\ Sを使用する必要があります。http://stackoverflow.com/を質問/ 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

wwwの前に自動的にhttp://が追加されるわけではないので、完全に正しいとは限りません.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); 

第3に、キャプチャカッコを使用すると、不必要に遅くなるだけです。それらを取り除く。 $1$0に更新することを忘れないでください。あなたが不思議に思われる場合には、これらは非キャプチャ括弧((?:))です。

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

最後に、私は\sの反対で短く、より正確な\S、と[^ ]+を置き換えます。 [^ ]+は空白を許可しませんが、改行とタブを受け入れることに注意してください。 \Sはありません。

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

スペースの代わりに改行で区切られた文字列に含まれる複数のURLがある場合、あなたは

preg_replace('/((www|http:\/\/)\S+)/', '<a href="$1">$1</a>', $val); 
関連する問題