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
how to preg replace "£200"
Moderator: General Moderators
Re: how to preg replace "£200"
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:
If you just need the money amount:
Code: Select all
$title = preg_replace('/£([0-9]+\.[0-9]{2})/i', '£<span>\1</span>', $title);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"
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'
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"
Hi,
yeah you would have to do it like that:
Greets.
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);
-
markjohnson
- Forum Newbie
- Posts: 15
- Joined: Sat Feb 07, 2009 8:36 pm
Re: how to preg replace "£200"
Code: Select all
$price = preg_replace('/£([0-9]+(\.[0-9]{2})?)/i', '£<strong>\1</strong>', $price);$price = '£99'
Re: how to preg replace "£200"
Just took that code and put it on my server:
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
Code: Select all
$price = '£99';
$price = preg_replace('/£([0-9]+(\.[0-9]{2})?)/i', '£<strong>\1</strong>', $price);
echo $price;[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