2

PHP の preg_replace 関数で使用する適切なパターンがわかりません。これは phpBB フォーラムの BBCode の例です。

[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]

テキストに埋め込まれている可能性のある開始フォントタグと終了フォントタグをいくつでも削除したいのですが、その間にテキストを入れておきます。この場合、私は次のようになりたいと思います:

[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]

フォントの名前はさまざまで、名前にスペースが含まれているものと含まれていないものがあります。パターンマッチは関係なく機能する必要があります。

前もって感謝します!

4

1 に答える 1

2

それらを単純に削除する場合は、複雑すぎる正規表現は必要ありません...

$str = '[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]';

$regex = '/\[font=.*?\]|\[\/font\]/i';

$str = preg_replace($regex, '', $str);

var_dump($str);

出力

string(457) "[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We donÕt want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]"

ideone でご覧ください

于 2011-02-04T01:56:17.990 に答える