osoclever
osoclever

Reputation: 373

Preg_replace and custom Tags

I have created a custom tag that needs to be stripped out and replaced before a page gets rendered. The tag looks like this: [@ customTagToBeReplaced]

The preg_replace pattern I am using is like this in php:

$pattern = "/\[@ .*\]/is";

When I have implemented it, sometimes more or less characters are stripped leaving half of an html tag broken. Like this:

before:

<div class="">
[@ error]
</div>

after:

<div class="</div>

Upvotes: 0

Views: 627

Answers (1)

freedev
freedev

Reputation: 30067

Your pattern is too greedy, try with something more specific:

$pattern = "/\[@ \w+\]/is";

\w Matches any word character (alphanumeric & underscore).

Upvotes: 1

Related Questions