views:

342

answers:

5

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" }
A: 

Use double backslashes. \

Anatoliy
If I did that that would also allow the backslash
Phill Pafford
No, this need to escape backslash char in string. To allow backslash in regex u should use four backslashes )
Anatoliy
No the code works as stated below form other people testing it and I have used this before in other scripts, it does not need a double backslash to work.
Phill Pafford
Actually regular expression you need works fine without double backslashes, but in php regular expression has described by a string, and to use one slash in string you need type two slashes, if you want to use two slashes in regular expression, you need four slashes in string, etc.. Hope, it's clear.
Anatoliy
He doesn't want to match any backslashes in his string. -1
Steven Oxley
Steven, FYI: double backslashes in php regex will not match any backslash. In the topicstarter's case double backslashes needed to escape - (minus).
Anatoliy
+1  A: 

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 ?

Pascal MARTIN
Hmm that's what I thought but for some reason it's not working. Would it matter in the value came from an Object?
Phill Pafford
Not sure what you mean by "came from an object" ; could you post the output of the var_dump I suggested ? (you can edit your post, for that -- will probably be easier to read than in a comment ^^ )
Pascal MARTIN
var_dump shows string
Phill Pafford
hmm I just noticed the size of the string does not equal the size for the regex. It's not showing up as white space cause I do a trim and have checked by adding some quotes around the value and printing it out to the screen.
Phill Pafford
Yep, the output of your var_dump is quite odd ; for me, var_dump('123-123-123-12345'); gives a length of 17 characters ;-) ;; maybe this is the cause of your problem, as your regex is using ^ and $ ?
Pascal MARTIN
One other thing, would it matter if the value was coming from a SimpleXMLElement Object?
Phill Pafford
Yep I used strip tags as it was coming from SimpleXML and now it validates just fine. Thanks for the help to put me in the right direction.
Phill Pafford
you're welcome :-) And nice you found out !
Pascal MARTIN
+1  A: 

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)

bisko
I thought that the dash or hyphen would cause the regex to think it's a range of something, this is why I escaped it.
Phill Pafford
One other thing, would it matter if the value was coming from a SimpleXMLElement Object?
Phill Pafford
Possibly, check the encoding of the source, and just in case try converting to UTF-8, as I am not sure how exactly SimpleXMLElement handles encoding of documents. I have played a bit with it but only with utf-8 encoded xml files and had no trouble.
bisko
Yep I used strip tags as it was coming from SimpleXML and now it validates just fine.
Phill Pafford
A: 

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

jeroen
sry that's just a typo. they are the same in the code, will change this in the question. Thnx
Phill Pafford
A: 

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?

chaos