Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.