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 ile http: // veya www nasıl değiştirilir.
preg_replace('~((www|http://)[^ ]+)~', '<a href="$1">$1</a>', $str);
Üçüncüsü, gereksiz yere parantezleri kullanarak gereksiz yere paraziti kullanmanız gerekir. Onlardan kurtulmak. $1
’u $0
’a güncellemeyi unutmayın. Merak ediyorsanız, bunlar yakalama parantezleri değildir: (?:)
.
preg_replace('~(?:www|http://)[^ ]+~', '<a href="$0">$0</a>', $str);
Son olarak, \s
tersidir daha kısa ve daha doğru \S
ile [^ ]+
değiştirmek. [^ ]+
'un boşluklara izin vermediğini, ancak yeni satırları ve sekmeleri kabul ettiğini unutmayın! \S
yapmıyor. bir dize bir yerine alanı satır sonu ile ayrılmış bulunan çok sayıda URL varsa
preg_replace('~(?:www|http://)\S+~', '<a href="$0">$0</a>', $str);
Dup: http://stackoverflow.com/ sorular/507436/nasıl yapılır-php-in-a-string-with-php ile –