Jump to content

how to preg replace "£200"


markjohnson

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/246472-how-to-preg-replace-%C2%A3200/
Share on other sites

I would use a preg_replace. Not sure if you will have to use html code for the £ symbol though?

 

preg_replace("/(£[0-9]+?\.[0-9]{2,2})/is", "<tag>\1</tag>", $title)

 

I've assumed that you will always have at least 1 preceeding number before the decimal place and that you will always have 2 numbers after it to fall with standard currency format (i.e. 0 pounds and 0 pence will be represented as £0.00). The \1 is a backreference to the first set of paranthesis (first round brackets basically), I think $1 is also another way of calling a backreference. Let me know if it works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.