Question

The values will be in this format 123-123-123-12345 that I would like the preg_match to work for. Can you see anything wrong with this regEx?

foreach($elem as $key=>$value) {

   // Have tried this With and without the + before the $ as well
   if(preg_match("/^[0-9]{3}\-[0-9]{3}\-[0-9]{3}\-[0-9]{5}+$/", $value)) {
      echo "Yeah match Elm: ".$value."<br />"; 
   } else {
      echo "Boo Hoo Elm: '".$value."'<br />";  
   }
}

have also tried

/^\d{3}\-\d{3}\-\d{3}\-\d{5}+$/

            //With and without the + before the $

They all fail with Boo Hoo ;(

EDIT:

var_dump($elem)

array(3) { [0]=>  string(1) "c" [1]=>  string(0) "" [2]=>  string(36) "123-123-123-12345" }
Was it helpful?

Solution

Trying this code :

$value = '123-123-123-12345';

if(preg_match("/^[0-9]{3}\-[0-9]{3}\-[0-9]{3}\-[0-9]{5}+$/", $value)) {
    echo "Yeah match Elm: ".$value."<br />"; 
} else {
    echo "Boo Hoo Elm: '".$value."'<br />";  
}

(Not sure the \ are usefull - but they don't seem to cause any trouble, in this particular case)

I get :

Yeah match Elm: 123-123-123-12345

And with this :

$value = '123-123-1a-123';

I get :

Boo Hoo Elm: '123-123-1a-123'

The regex does actually seem to work for me ?


Could you provide a bit more code ? Or maybe use :

var_dump($elem);

might be usefull, to check if it really contains what you are expecting ?

OTHER TIPS

Could you please provide some test array with the data (serialized would be best) as I cannot reproduce this behaviour.

$elem = array ('123-123-123-12345');

foreach($elem as $key=>$value) {

   // Have tried this With and without the + before the $ as well
   if(preg_match("/^[0-9]{3}\-[0-9]{3}\-[0-9]{3}\-[0-9]{5}+$/", $value)) {
      echo "Yeah match Elm: ".$value."<br />"; 
   } else {
      echo "Boo Hoo Elm: '".$value."'<br />";  
   }
}

result: Yeah match Elm: 123-123-123-12345

Also the backslashes are not needed in the regular expression. The dashes only need to be escaped in [] groups if they are not the first character ([-a-z] would match "- OR a-z", but [a-z] would match "a or \ or z").

--- EDIT ---

Ok, so the only thing that I cannot understand is what encoding are you using ?

string(36) "123-123-123-12345"

36 bytes... The closest I got is UTF-16, but got only 34 bytes. So what is the encoding you are using?

You can also try to convert the string to utf-8 before matching with the regexp. (also try using the 'u' parameter in the regexp: '/somreg\d/u', after converting to utf-8)

Use double backslashes. \

Ummmmmm....

In your edit you are talking about $array and in your loop about $elem.

Could that be the problem?

Edit: By the way, there is something strange with your data, I count only 17 characters so why is is giving a string (36)?

Unable to replicate. Your code works fine when I paste it into a file.

Since your var_dump() shows your target string is 35 bytes for a 15-character string, though, it does seem like you have encoding issues. What happens if you run the regex against utf8_decode($value) instead of $value?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top