Dash Problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
rover
Forum Newbie
Posts: 3
Joined: Sat Mar 07, 2009 6:09 pm

Dash Problem

Post by rover »

Hi there,

I use CakePHP + WAMP on Windows.
My problem is that whenever there is a dash in a pattern and an input string has a dash, no match occurs.
As a result, validation fails.

Here's an example of the code I used:

Code: Select all

 
function valid_phone($data) {
  $pattern = "^[0-9]{3}-[0-9]{4}$";
  $phone_no = trim($data['phone']);
  if (ereg($pattern, $phone_no)) 
        return 1;
  else 
        return 0;
}
 
If I remove the dash from the pattern and input 7 digits, everything works fine.
If I add the dash and input something like "123-45657", validation fails.

This is not the only pattern I've tried, all with the same results.

Any ideas?

Thank you!
Last edited by rover on Sat Mar 07, 2009 7:29 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Dash Problem

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Dash Problem

Post by atonalpanic »

Your input string, the phone number, is more than 7 digits. Also if you are using the === operator to check
what this function returns, it will fail. Other than that, I had no problem at all running this simplified version of your code.

Code: Select all

 
$pattern = "^[0-9]{3}-[0-9]{4}$";
$phone_no = '123-4567';
 
echo ereg($pattern, $phone_no);
 
rover
Forum Newbie
Posts: 3
Joined: Sat Mar 07, 2009 6:09 pm

Re: Dash Problem

Post by rover »

atonalpanic,

Thank you for simplifying this for me. So the issue is not regex at all. It is the dashes. An input dash from my form and a dash stored in a variable are not being enterpreted as equal, for some reason.

Thanks!
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Dash Problem

Post by atonalpanic »

I wouldn't say that. I would say that your input contains too many digits. Your input string contained 8, which is why it would not match.
rover
Forum Newbie
Posts: 3
Joined: Sat Mar 07, 2009 6:09 pm

Re: Dash Problem

Post by rover »

atonalpanic wrote:I wouldn't say that. I would say that your input contains too many digits. Your input string contained 8, which is why it would not match.
What I meant before was when I didn't have a dash in the pattern, just [1234567], and then submitted a form with 7 digits it worked fine. But when I changed the pattern and input to include the dash, it would fail.

I figured it out. My sanitize function from CakePHP was changing my input, so that a dash from my form and a dash in my code were not the same character anymore.

Your simplified way of looking at things helped me do that :)
Post Reply