Page 1 of 1

how to preg replace "£200"

Posted: Mon Sep 05, 2011 8:28 am
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

Re: how to preg replace "£200"

Posted: Mon Sep 05, 2011 9:51 am
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];
}

Re: how to preg replace "£200"

Posted: Mon Sep 05, 2011 10:06 am
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'

Re: how to preg replace "£200"

Posted: Mon Sep 05, 2011 10:16 am
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.

Re: how to preg replace "£200"

Posted: Mon Sep 05, 2011 10:26 am
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'

Re: how to preg replace "£200"

Posted: Mon Sep 05, 2011 10:39 am
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