php - Preg_replace pattern for BBCode quote tag -


i want use php preg_replace function on 2 strings unsure of regular expression use.

for first string, require author value (so after author=, nothing after space):

[quote author=username link=1150111054/0#7 date=1150151926] 

result:

[quote=username] 

for second string, there no author= tag. username appears after closed open quote

[quote] username link=1142890417/0#43 date=1156429613] 

ideally, result should be:

[quote=username] 

make string author= , ] optional inorder replacement on both type of strings.

regex:

^\[(\s+?)\]?\s+(?:author=)?(\s+).*$ 

if want mention string quote on regex use this,

^\[(quote)\]?\s+(?:author=)?(\s+).*$ 

replacement string:

[$1=$2] 

demo

<?php $string =<<<eot [quote author=username link=1150111054/0#7 date=1150151926] [quote] username link=1142890417/0#43 date=1156429613] eot; echo preg_replace("~^\[(\s+?)\]?\s+(?:author=)?(\s+).*$~m", "[$1=$2]", $string); ?> 

output:

[quote=username] [quote=username] 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -