2009-07-04 36 views
8

I've created this regex<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/ प्रश्न/507436/कैसे-करें-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

मजेदार है कि अधिकांश ने आपके उदाहरणों के लिए आपके गलत regex का उपयोग किया है। – Gumbo

+0

मुझे कल्पना है कि हम सभी ओपी की प्रतिलिपि बना रहे थे। किसी भी घटना में अब फिक्स्ड। – 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

मैं बस जोड़ना चाहता हूं कि बैक संदर्भों को शुरुआती कोष्ठक द्वारा क्रमांकित किया गया है। उदाहरण के लिए, regexp में "टी (ई (सेंट))", \ 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 के साथ http: // या www को कैसे बदलें।

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 नहीं करता है।

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

कई यूआरएल एक स्ट्रिंग एक एक अंतरिक्ष के बजाय एक लाइन ब्रेक के द्वारा अलग में निहित नहीं है, तो आप \ एस का उपयोग करने के

preg_replace('/((www|http:\/\/)\S+)/', '<a href="$1">$1</a>', $val); 
संबंधित मुद्दे