regex - PHP and preg_replace -
i have text parsing this:
some text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. text. [attachment=0]winter.jpg[/attachment]some text. text. text. text. text. text. text. text. text. text. text. text.
i want match , remove instance of text string:
[attachment=0]winter.jpg[/attachment]
where winter.jpg can text.
however, getting php notices. used regexpal.com construct this, works there uses javascript regex function:
\[attachment=.*?].*\[/attachment]
when run code:
$pm_row['message_text'] = preg_replace('\[attachment=.*?\].*\[/attachment\]', '', $pm_row['message_text']);
php complains notice:
[phpbb debug] php notice: in file /mail_digests.php on line 841: preg_replace() [function.preg-replace]: delimiter must not alphanumeric or backslash
so on similar line of code, delimit pattern "/":
$post_row['post_text'] = preg_replace('/\[attachment=.*?].*\[/attachment]/', '', $post_row['post_text']);
but generates following:
[phpbb debug] php notice: in file /mail_digests.php on line 957: preg_replace() [function.preg-replace]: unknown modifier 'a'
any ideas how fix this?
you need escape every occurrence of delimiter inside pattern:
if delimiter needs matched inside pattern must escaped using backslash. if delimiter appears inside pattern, idea choose delimiter in order increase readability.
so escape it:
'/\[attachment=.*?].*\[\/attachment]/' ^
by way: quantifier in .*
greedy, means match as possible. might want change ungreedy variant using ?
did before.
Comments
Post a Comment