473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

regexps: dollar sign, lookaheads/behinds and speedquestions

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
2 3018
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ding | last post by:
Hello, I've got a problem with the regular expression api. To illustrate the problem i've made a little example: Matcher m=Pattern.compile("test").matcher("this is a test not");...
29
by: Mark Hahn | last post by:
We are considering switching to the dollar sign ($) for self, instead of the period ( . ) we are using now in Prothon. Ruby uses the at-sign (@) for self, but our new usage of self also includes...
3
by: Monsey Computers | last post by:
In some searches on our web site the items will come back with a different dollar sign than the US $ symbol why is that? MC *** Sent via Developersdex http://www.developersdex.com *** Don't...
3
by: mark.a.lemoine | last post by:
I need to be selecting records based on the value of a string-format currency value. Specifically, I have a table with a field of type varchar(50). Stored in this field is a dollar-formatted...
17
by: Digital Puer | last post by:
I've inherited some code where the coder placed dollar signs in his preprocessor macros. What is the significance of the dollar signs ($) ? Example: #define ALLOCATE(task,pointer) \ { \...
2
by: johnson4 | last post by:
Im wanting to display a dollar sign on my wap application, which requires that the cell phone is served with $$ to display $. Therefor I'm trying to find an easy way, in my php code, that will...
14
by: gimme_this_gimme_that | last post by:
What is going on here with the dollar signs and parenthesis? $(document).ready(function(){ $("li").behavior("click",function(){ $(this).load(menu.html"); }); }); And when do you use click...
29
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
4
by: Robbie Hatley | last post by:
Some months ago, I'd asked this group: "Backtick, at-sign, dollar-sign: legal in source?" I got 20 replies, most of which were off-topic and did not answer the question. Keith Thompson...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.