Matching placeholders

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

bartz
Forum Newbie
Posts: 11
Joined: Thu Aug 31, 2006 12:45 pm

Post by bartz »

Please correct me if I am wrong, but doesn't (\d)+ matches one or more digits? So it'll still match the 1 of 10?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

sorry, typo

Code: Select all

<?php

$a = array(1=>'x', 2=>'y', 11=>'z');

echo preg_replace('!(\d+)!e', '$a[\\1]', '1|2|11');
?>

maybe unrecognized (because I edited too late on page 1):
volka wrote:
bartz wrote:Yes, I know that, but I'm building for a generalised DB abstraction, to easily swap out databases, using an Abstract Factory, and derived classes for each database flavour. So, I definately want to do my handling in PHP.
pdo provides all of that. http://www.php.net/pdo

Last edited by volka on Fri Sep 01, 2006 13:27; edited 1 time in total
bartz
Forum Newbie
Posts: 11
Joined: Thu Aug 31, 2006 12:45 pm

Post by bartz »

Could you be so kind as to explain the difference between (\d)+ and (\d+)?

And yes, I am aware that PDO does all that, but my lib has some additional features on which an entire app is built, so it's not very easy to swap my solution out for PDO/Pear::DB/and so on.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

(\d+) : one or more digit(s) as one group
(\d)+ : I would have expected this to be an error. Don't no what it is.

You may also forget about the whole grouping thing (in this case) and use

Code: Select all

<?php
$a = array(1=>'x', 2=>'y', 11=>'z');

echo preg_replace('!\d+!e', '$a[\\0]', '1|2|11');
?>
bartz wrote:And yes, I am aware that PDO does all that, but my lib has some additional features on which an entire app is built, so it's not very easy to swap my solution out for PDO/Pear::DB/and so on.
wouldn't it be faster/easier/more stable anyway to use your class only as some kind of app<->pdo compatibilty layer and maybe replace it completely in the future?
bartz
Forum Newbie
Posts: 11
Joined: Thu Aug 31, 2006 12:45 pm

Post by bartz »

No, I'm fully confident in my code, and all the additional features. It's just that I encountered this bug for the first time now. Will try your solution now.
bartz
Forum Newbie
Posts: 11
Joined: Thu Aug 31, 2006 12:45 pm

Post by bartz »

Thanks, your solution works, with some tweaks.
For the record, this is the line that made things work:

Code: Select all

$query = preg_replace('/\:(\d+)/e', '$binds[\\1]', $query);
Topic may be closed.
Post Reply