2009-07-04 5 views
8

I've created this regexSo ersetzen Sie http: // oder www durch <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 verwenden: http://stackoverflow.com/ Fragen/507436/how-do-i-linkify-URLs-in-String-mit-PHP –

Antwort

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

Herzlichen Glückwunsch! Du bist der einzige, der die falsche Regex nicht aus dem Chaos kopiert. – Gumbo

+1

Es fügt nicht automatisch http: // vor www hinzu, also ist es nicht ganz richtig: www.google.com wird ... 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

Lustig, dass die meisten Ihre falsche Regex für ihre Beispiele verwendet. – Gumbo

+0

Ich kann mir vorstellen, dass wir alle OPs kopiert haben. Auf jeden Fall jetzt behoben. – 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

Ich möchte nur hinzufügen, dass Rückbezüge durch die öffnende Klammer nummeriert sind. Zum Beispiel, in der Regexp "T (e (st))", enthält \ 1 "est", und \ 2 enthält "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.

Drittens verwenden Sie unnötige Klammern, die nur die Dinge verlangsamen. Sie loswerden. Vergessen Sie nicht, $1 zu $0 zu aktualisieren. Falls Sie sich wundern, sind dies nicht einfangende Klammern: (?:).

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

Schließlich möchte ich [^ ]+ mit dem kürzeren und genauer \S, ersetzen, die das Gegenteil von \s ist. Beachten Sie, dass [^ ]+ keine Leerzeichen erlaubt, aber Zeilenumbrüche und Tabulatoren akzeptiert! \S nicht.

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

Wenn mehrere URL in einem String durch einen Zeilenumbruch statt eines Raumes ein getrennt enthalten sind, müssen Sie die \ S

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