By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

regexps: dollar sign, lookaheads/behinds and speedquestions

P: n/a
I just started to try regexps in php and I didn't have too many
problems, however I found a few when trying to build a templte engine.

The first one is found is the dollar sign. In my template I would like
to write this:

{$var} where var ofcourse will be replaced by the real var however I'm
having trouble with the dollar sign I do escape the it though, my
regexp:

preg_replace("/\{\$(.+?)\}/", 'var', $string);

However this didn't work, even if I try to just replace the dollar
sign (by a b) it doesn't work properly:

preg_replace("/\$/", "b", $string);

Another question is about lookaheads/behinds, in my template I would
like to use this:

{include file="file.tpl"} for example, so I created this regexp:
preg_replace("/\{include file=(?:\"|\')*(.+?)(?:\"|\')*\}/i",
'replacement', $string);

Works fine but now people don't have to use the same opening and
ending character, how do I do that?

Last questions for now, I know regexps are not very fast and this does
give a bit of a problem.

So my questions:
- Are there things in regexps te be avoided since it takes ages?
- Are there ways to speed up regexps?

Feb 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You can use single quotes to escape the dollar:

'/\{\$(.+?)\}/'

For the other case, you could do an or with a pipe:

'/\'.*?\'|".*?"/'

On Feb 25, 2:25 pm, "Yorian" <yorianbenja...@hotmail.comwrote:
I just started to try regexps in php and I didn't have too many
problems, however I found a few when trying to build a templte engine.

The first one is found is the dollar sign. In my template I would like
to write this:

{$var} where var ofcourse will be replaced by the real var however I'm
having trouble with the dollar sign I do escape the it though, my
regexp:

preg_replace("/\{\$(.+?)\}/", 'var', $string);

However this didn't work, even if I try to just replace the dollar
sign (by a b) it doesn't work properly:

preg_replace("/\$/", "b", $string);

Another question is about lookaheads/behinds, in my template I would
like to use this:

{include file="file.tpl"} for example, so I created this regexp:
preg_replace("/\{include file=(?:\"|\')*(.+?)(?:\"|\')*\}/i",
'replacement', $string);

Works fine but now people don't have to use the same opening and
ending character, how do I do that?

Last questions for now, I know regexps are not very fast and this does
give a bit of a problem.

So my questions:
- Are there things in regexps te be avoided since it takes ages?
- Are there ways to speed up regexps?

Feb 25 '07 #2

P: n/a
Yorian wrote:
However this didn't work, even if I try to just replace the dollar
sign (by a b) it doesn't work properly:

preg_replace("/\$/", "b", $string);
You need to either use single-quote marks, or double-escape it.

"/\\\$/"
'/\$/'

This is because the dollar sign has two special meanings in this case.
Dollar signs in double-quotes are used by PHP for interpolation, so need
escaping. Dollar signs in PCREs are used to mark the end of a string, so
need escaping. Hence the double-escape. (i.e. three slashes!)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.