preg_replace требуется помощь для BBCode - PullRequest
2 голосов
/ 04 февраля 2011

Я не могу понять, какой именно шаблон можно использовать с функцией PHP preg_replace.Вот пример некоторых BBCode на форуме phpBB.

[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]

Названия шрифтов могут быть разными, у некоторых в именах есть пробелы, у некоторых - нет.Сопоставление с шаблоном должно работать независимо.

Заранее спасибо!

1 Ответ

2 голосов
/ 04 февраля 2011

Если вы просто удаляете их, вам не нужно слишком сложное регулярное выражение ...

$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 .

...