Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm going through my code to replace any instances I'm using the ereg() function - which I was using for matching regex inside a string.

I could use a little direction, if someone has a better method than what I'm using.

Here's my old "currency validation" script:

    function valid_currency($number){
     if(ereg('^[0-9]+.[0-9]{2}$', $number))
      return true;
     else
     return false;
    }

    if(valid_currency(25.30)){ 
          echo "valid currency"; 
   }else{ 
          echo "invalid currency string"; 
   }

I replaced the ereg() with preg_match().

I'm getting this error now:

Warning: preg_match() [function.preg-match]: No ending delimiter '^'

I'm guessing the regular expression syntax isn't being recognized. From here I'm a little stuck.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
945 views
Welcome To Ask or Share your Answers For Others

1 Answer

preg requires delimiters around your regex. It can be almost anything though traditionally it's /. This should work:

preg_match('/^[0-9]+.[0-9]{2}$/', $number)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...