Jump to content


Photo

preg_match Error


  • Please log in to reply
4 replies to this topic

#1 CaptainC

CaptainC
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 March 2006 - 02:40 PM

I have an error with this statement:

preg_match("[0-9]+\.?[0-9]*", $text_string, $match_array);
[!--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?

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 28 March 2006 - 03:22 PM

You forgot to add delimiters to the regex.
preg_match("/[0-9]+\.?[0-9]*/", $text_string, $match_array);
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.

#3 CaptainC

CaptainC
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 March 2006 - 03:57 PM

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" ?

#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 28 March 2006 - 05:05 PM

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" ?

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.
/[0-9]+(.[0-9]+)?/


#5 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 28 March 2006 - 05:33 PM

You can use this:

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users