/    Sign up×
Community /Pin to ProfileBookmark

Help with regular expression.

Hello everyone, I am building a templating system for my custom CMS and I want to replace tags that begin with { and end with } in HTML files with my own text.

I did this using the following regular expression
$render_page = preg_replace(‘/{+.*?}/im’,”, $render_page);

Now the problem that I am facing is that some times usefull text that it is not a translation tag is included in {}.

The most usefull solution I can think of is to restrict tags to not contain any spaces so {useful_tag} wil be replaced but { start_with_spaces } or {contains spaces} will not get replaced.

Could you please help with the regular expression?

Other suggestions are also welcome.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 28.2010 — <i>
</i>'/{S+?}/'

[B]S[/B] (upper-case) is any non-white-space character.
Copy linkTweet thisAlerts:
@paishinauthorJan 28.2010 — wow! I cant believe how fast this was ? I posted the question less than 15 minutes away.

Thanks NogDog, it works flawlessly ?
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 28.2010 — This is not a followup to the solution, just food for thought.

Instead of worrying about spaces plus the fact that it is possible a user might include {text} in their content if you're also parsing content (it's possible) switch to very unique tags, i.e.

<!-- tag: name -->

<% asp style %>

||: tag name ?|

The first is an HTML comment which is really interesting to use because even if a user includes HTML comments, a collision with your tags is very unlikely, plus any tags that don't get parsed remain in the content as HTML comments, not visible to the end user unless they view source.

Hope I'm making sense here. If content parsing is not an issue, disregard.

-jim
×

Success!

Help @paishin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...