2

$str :: のような文字列があるとしましょう

$str = "Test,

Here\'s an interesting in-house litigation position with JPMorgan Chase in New York I thought you might be interested inL

<a href="http://www.example.com/lcdetail.php?akey=e1725">http://www.example.com/lcdetail.php?akey=e1725</a>

They are not using recruiters to fill this job, so if you are interested you can just apply directly.  

http://www.example.com/lcdetail.php?akey=e1725

Cordially
--Harrison";

タグが存在しないリンクにタグを追加したいと思います。

この関数を使用しましたが、目的の値が返されませんでした。

function processString($s){  
  return preg_replace('@(?<!href=")(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)*@i','<a href="$1">$1</a>',$s);
}

私の要件は、アンカータグが存在する場合はリンクに追加し、存在しないものには追加しないことです。

ありがとう。

4

1 に答える 1

4

(?<!href="|">)さて、タグ内のリンクを避けるために、後読みパスを変更しました...

だから使用:

function processString($s){  
  return preg_replace('@(?<!href="|">)(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)*@i','<a href="$1">$1</a>',$s);
}

正規表現101のデモ

コードパッドのデモ

于 2013-09-09T12:53:18.050 に答える