Hello everyone,

I encountered a problem for using the preg_replace method in a text which content a price formatted as $123.45.

<?php
$xRegex = '#{params:.+?}#s';
$sPrice = "$123.45";
$sContent = "Price is {params:xxx}.";
echo preg_replace( $xRegex, $sPrice, $sContent, 1 );
?>

When i'm doing this, I got : Price is 3.45

After some research on the PHP documentation, and if I undesrtood correctly, preg_replace consider $ as a reference.
The only I way I could get over this problem is by using urlencode() and urldecode() as following:

<?php
$xRegex = '#{params:.+?}#s';
$sPrice = "$123.45";
$sContent = "Price is {params:xxx}.";
echo urldecode(preg_replace( $xRegex, urlencode($sPrice), $sContent, 1 ));
?>

Is there an other way for doing this? Am I thinking this correctly?

Thanks for any ideas or advices!

[Rant = My favorite Subject]

simplify the database
change any text dates "October 15 2008"(smalltext 15 characters) etc to timestamps (Int(10))
strip out the $signs
you only need them when you print out information echo '$ '.$sPrice; is just as fast and causes no $_ errors
the database should contain
no formatting
no text dates or times
nothing but data in its basic form,
its easier to manipulate, smaller, faster, cheaper to maintain
and you can calculate on the fields, which is more difficult to do with text representations,
not so much now for 50 items, 50Bytes extra per row isnt much
but when the db grows to 500 000 items and there are millions of transactions in the transaction table, 50Bytes per row is a lot

[/Rant]

... ... ... holy crap what the heck are you doing? You just crushed an ant with Mt. Everest.

If you're trying to remote the $

$string = str_replace('$', '', $price);

In regular expressions $ is not a reference. It means End of Line. so the regex s$ would match a line with s at the end. You can escape it just like any other character with a backslash.

Hi everyone,

Thanks for your reply.
@ almostbob : I'm not sure I understood your answer. I don't have any DB in my example.

@ ShawnCplus : Ok, I understand better but what I have to do if i want to display a price in a sample text with $ entity.

To be more specific, I have a text with differents personal tags as {params:id=1} and I'm trying to replace it by different values. But when it is a price, it doesn't work.
What I would like to see is this : Price is $123.45

I'm sorry if I don't explain myself correctly.

See ya!

@ ShawnCplus : I red again PHP doc and I found this

replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern.

I tried with this pattern

<?php $sPrice= "\\$123.45" ?>

and it works!

So thanks and maybe this will be useful to others ;)

Thats why its wrapped in [rant][/rant] tags,
may not be strictly relevant to this post, sorry,
I get so many 'fix my database requests',
where the data is input with dollar signs,
as text dates
as proprietary codes
then the user wonders why it dont work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.