0

任意の http/url からタグを消去する PHP ソリューションを考え出す正規表現の忍者はいますか?ただし、テキストの残りの部分にはタグを残しますか?

例えば:

the word <cite>printing</cite> is in http://www.thisis<cite>printing</cite>.com

なる必要があります:

the word <cite>printing</cite> is in http://www.thisisprinting.com
4

3 に答える 3

1

これは私がすることです:

<?php
//a callback function wrapper for strip_tags
function strip($matches){
    return strip_tags($matches[0]);
}

//the string
$str = "the word <cite>printing<cite> is in http://www.thisis<cite>printing</cite>.com";
//match a url and call the strip callback on it
$str = preg_replace_callback("/:\/\/[^\s]*/", 'strip', $str);

//prove that it works
var_dump(htmlentities($str));

http://codepad.viper-7.com/XiPCS9

于 2013-10-24T21:56:18.737 に答える
1

この置換に適切な正規表現は次のようになります。

#(https?://)(.*?)<cite>(.*?)</cite>([^\s]*)#s
  1. sすべての改行で一致するフラグ。

  2. タグ間の選択を使用しlazyて、より類似したタグをエスケープしないように正確にします

スニペット:

<?php
$str = "the word <cite>printing<cite> is in http://www.thisis<cite>printing</cite>.com";
$replaced = preg_replace('#(https?://)(.*?)<cite>(.*?)</cite>([^\s]*)#s', "$1$2$3$4", $str);
echo $replaced;

// Output: the word <cite>printing<cite> is in http://www.thisisprinting.com

ライブデモ

于 2013-10-24T22:02:35.653 に答える