how to preg replace "£200"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markjohnson
Forum Newbie
Posts: 15
Joined: Sat Feb 07, 2009 8:36 pm

how to preg replace "£200"

Post by markjohnson »

If I have a string as such:
$title = "Product Title £143.99"

How can I search the string for £ and then all the subsequent numbers, and then enclose them with a tag?

I was thinking of using preg match, and enclosing the result within whatever tag.

Any ideas?

Thanks
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: how to preg replace "£200"

Post by twinedev »

Not quite sure what you mean by enclosing it with a tag, so I'm assuming you mean like wrapping it with a <span> tag:

Code: Select all

$title = preg_replace('/£([0-9]+\.[0-9]{2})/i', '£<span>\1</span>', $title);
If you just need the money amount:

Code: Select all

if (preg_match('/£([0-9]+\.[0-9]{2})/',$title,$regs)) {
    $price = $regs[1];
}
markjohnson
Forum Newbie
Posts: 15
Joined: Sat Feb 07, 2009 8:36 pm

Re: how to preg replace "£200"

Post by markjohnson »

Hi,

Thank you very much for the response.

It is the first suggestion I am after. However, I tried it as such:

$price = preg_replace('/£([0-9]+\.[0-9]{2})/i', '£<strong>\1</strong>', $row_Product['price']);

But no joy.

$row_Product['price'] = '£99'
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: how to preg replace "£200"

Post by genix2011 »

Hi,

yeah you would have to do it like that:

Code: Select all

$title = preg_replace('/£([0-9]+(\.[0-9]{2})?)/i', '£<span>\1</span>', $title);
Greets.
markjohnson
Forum Newbie
Posts: 15
Joined: Sat Feb 07, 2009 8:36 pm

Re: how to preg replace "£200"

Post by markjohnson »

Code: Select all

$price = preg_replace('/£([0-9]+(\.[0-9]{2})?)/i', '£<strong>\1</strong>', $price);
Still no joy for some reason.

$price = '£99'
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: how to preg replace "£200"

Post by twinedev »

Just took that code and put it on my server:

Code: Select all

$price = '£99';

$price = preg_replace('/£([0-9]+(\.[0-9]{2})?)/i', '£<strong>\1</strong>', $price);

echo $price;
Here is what I received:

[text]£<strong>99</strong>[/text]

So it makes me wonder if $price really contains what you think it does? Maybe have whitesapce before or after?

-Greg
Post Reply