Benn
Benn

Reputation: 5023

Preg replace everything in square brackets including brackets but watch for special chars

I have several shortcodes that are generating tabs, accordions ,links image gallery, icons etc and were able to match their code and remove them where necessary , but I ended up removing German or Russian special chars as well.

shortcode

<br/>
[tabs id="myid" type="tabnav"]<br/>
[tabsgroup title="Tab title" active="1"]Tab für goes here...[/tabsgroup]<br/>
[tabsgroup title="Tab title" active="0"]Tab für goes here...[/tabsgroup]<br/>
[tabsgroup title="Tab title" active="0"]Tab Хёз но фырре доктюж котёдиэквюэ, нэ убяквюэ янжольэнж вэл ...[/tabsgroup]<br/>
[/tabs]<br/>

remove shortcodes

$cleanStr = preg_replace(array(
    '/parse/',
    '/pre/',
    '/imgs/',
    '/fa/',
    '/media/',
    '/stabsgroup/',
    '/stabs/',
    '/note/',
    '/acgroup/',
    '/acs/',
    '/stabs/',
    '/url="(.*?)"/',
    '/link="(.*?)"/',
    '/poster="(.*?)"/',
    '/width="(.*?)"/',
    '/height="(.*?)"/',
    '/resp="(.*?)"/',
    '/id="(.*?)"/',
    '/title="(.*?)"/',
    '/type="(.*?)"/',
    '/active="(.*?)"/',
    '/color="(.*?)"/',
    '/name="(.*?)"/',
    '/target="(.*?)"/',
    '/class="(.*?)"/',
    '/image="(.*?)"/',
    '/border="(.*?)"/',
    '/radius="(.*?)"/',
    '/icon="(.*?)"/',
    '/close="(.*?)"/',
    '/effect="(.*?)"/',
    '/days="(.*?)"/',
    '/hours="(.*?)"/',
    '/[^A-Za-z0-9?!\s]/i', // this here removes the brackets but it also removes  special chars
), array(
    ''
), $str);

I did try with one liner but it did not work in some cases so I had to line up the array like you see it above

here is one liner http://regex101.com/r/gY3pT3/1

and it works with g modifier for js but for php I would have to switch from preg_replace to preg match all and further complicate something that should be very simple

Replace everything inside [] or [] [/...] but leave text and special chars in tact.

Any insight is helpful.

Upvotes: 0

Views: 118

Answers (2)

Manoj Sonagra
Manoj Sonagra

Reputation: 45

This may work for you

preg_match_all('#\b(rain|dry|clear)\b#', $string, $matches);

Upvotes: 0

Michel
Michel

Reputation: 4157

This might work: replace this expression

'/[^A-Za-z0-9?!\s]/i'

with this expression

'/[^\p{L}|\p{N}|\s]+/u'

and it should leave the unicode characters alone.

The php manual has a usefull page on it

Upvotes: 1

Related Questions