Jump to content

Archived

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

CaptainC

preg_match Error

Recommended Posts

I have an error with this statement:

[code]preg_match("[0-9]+\.?[0-9]*", $text_string, $match_array);[/code]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: preg_match(): Unknown modifier '+' in ...[/quote]
I thought it had something to do with the slashed in php strings, but I have not been able to get it right.

Any thoughts?

Share this post


Link to post
Share on other sites
You forgot to add delimiters to the regex.
[code]
preg_match("/[0-9]+\.?[0-9]*/", $text_string, $match_array);
[/code]
This question is also more appropriate for the PHP-Help forum and will be moved. Always look for the most appropriate forum for your question before posting.

Share this post


Link to post
Share on other sites
Would this regex be the way to go if I wanted to pull out the number value from this example?:

<b><i>$456.50</i></b>

And have preg_match pull out "456.50" ?

Share this post


Link to post
Share on other sites
[quote]
Would this regex be the way to go if I wanted to pull out the number value from this example?:

<b><i>$456.50</i></b>

And have preg_match pull out "456.50" ?
[/quote]
If the string will only contain one number then it should be fine. The regex will match a number that ends with a "." however, which may not be what you want. You could change it to the following to avoid that.
[code]
/[0-9]+(.[0-9]+)?/
[/code]

Share this post


Link to post
Share on other sites
You can use this:

[code]preg_match('/\d*(\.\d+)?/', $text_string, $match_array);
$number = $match_array[0];[/code]
That will match '0.1', '3', and '.1'.

Share this post


Link to post
Share on other sites

×

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.