473,903 Members | 5,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3050
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.comwro te:
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
21340
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"); System.out.println(m.replaceAll("\\$_POST")); Works fine, prints:
29
22230
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 replacing the period for some attribute references, as in obj$func() versus obj.func(), and too many programs treat that as an email address and screw it up. Also the S in the symbol $ reminds one of the S in $elf. Can people from outside the...
3
1617
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 just participate in USENET...get rewarded for it!
3
2271
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 value, complete with the dollar sign and commas for the thousands separator (and an optional leading minus sign for negative values). For instance: $1,234.56. One restriction I have to live with is that I can't change the schema of the table or...
17
10479
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) \ { \ long task$; \ if (task == FOO) { \ task$ = ZERO); \
2
2329
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 always add a dollar sign next to any existing dollar sign. so that dollar signs are always served in pairs The content is residing in a MySQL database. Thanks for any help that can be offered.
14
9868
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 vs. onclick? Is onclick only an attribute thing?
29
19342
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 site and so far doing this has saved about 8KB of javascript (lots of ajax/dynamic elements). I just
4
4414
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 provided the only direct, on-topic reply. He wrote:
0
9999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9847
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11282
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10983
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10500
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7206
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.