markjohnson Posted September 5, 2011 Share Posted September 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
joe92 Posted September 5, 2011 Share Posted September 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
cags Posted September 6, 2011 Share Posted September 6, 2011 No real difference to joe92's pattern, just a slightly different take of... preg_replace('/£\d+(?:\.\d{2})/', "<tag>\0</tag>", $title); I think it should work, though I did just get up, so it's not tested Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.