...

View Full Version : Resolved Finding plus(+) sign in string



Jesper Møller
02-13-2010, 06:23 AM
Hi
Im rather new to PHP (only been at it a week) so i hope someone can help me

I want to check a string to see if its a valid phonenumber, so it may contain only numbers and spaces AND it may also contain a single plus(+) sign for contry code, but only at the beginning of the number

this is what i have come up with so far:

First chekking the string only have numbers, spaces and +sign

if (preg_match("/^([0-9 \+]+)$/", $string)) {

Then chekking that ther is only +sign

if (preg_match_all("/[\+]/", $string)==1) {
$check2 = 'Landcode ok<br>';
}
else
{
$check2 = 'Landcode NOT ok<br>';
}
and its this check i cant get to work ??? no mater

hivelocitydd
02-13-2010, 07:19 AM
Please try the following





if (preg_match("/^(\+)*([0-9]+)$/", $string)) {

echo "Okay";

} else {

echo "not a land number";
}

MattF
02-13-2010, 07:44 AM
if (preg_match("/^\+?([\d\s]+)$/", $string)) {

Jesper Møller
02-13-2010, 07:48 AM
Thanks :)

As i see it i wasn't totaly wrong
But that looks mutch easyer

Only thing missing was the space but got that with [0-9 ]

now i haw to find out hov/why that works ?

i gues (if i have read my tutorials corect) that the ^(\+) meens look for a + ONLY in the beginning
the * is like a + but may only ocur once
[0-9 ]+ is looking for number and spaces and the space meens the may ocur more than once
and the $ meen its shal keep on looking until the end

??

Jesper Møller
02-13-2010, 07:57 AM
Thanks ... thant workd to

almost the same .. but with \d for digits and \s for spaces right ??

but that ? and no () around \+ ???

MattF
02-13-2010, 08:02 AM
Thanks ... thant workd to

almost the same .. but with \d for digits and \s for spaces right ??

but that ? and no () around \+ ???

You aren't capturing the plus sign? No need for the capturing parantheses, if so. The ? in the expression states that the preceding character may or may not exist, but if it does it will match one, and only one, plus sign, whereas the * in the first example posted will match none of or any number of + at the beginning of the string. Try it with several ++ at the beginning of the string and you'll see what I mean. The \d and \s, as you correctly surmise, are merely shorthand.


Edit: In fact, you aren't capturing anything with that expression, so you can remove all parantheses:



if (preg_match("/^\+?[\d\s]+$/", $string)) {


If you want to use it as a check and capture expression, use:



if (preg_match("/^(\+?[\d\s]+)$/", $string, $matches)) {

Jesper Møller
02-13-2010, 08:13 AM
Your right .. didnt test with 2+ signs ind the begining

BTW.
Is it posible to "revers" that line so the if state only is tru if it dont match

Thinking loosing the else state
somthing like:
ifnot (preg_match("/^\+*([\d\s]+)$/", $string)) {
echo ('WRONG');
}

Jesper Møller
02-13-2010, 08:19 AM
You aren't capturing the plus sign? No need for the capturing parantheses, ......

Edit: In fact, you aren't capturing anything with that expression, so you can remove all parantheses:


Im not shure i know what is ment by capturing
dictionary says To take captive,seize, To gain possession or contro ??

Sorry my english aint that good (Im a little dyslexie)

MattF
02-13-2010, 08:20 AM
For a direct non match, use:



if (!preg_match("/^\+?[\d\s]+$/", $string)) {

MattF
02-13-2010, 08:23 AM
Im not shure i know what is ment by capturing

If you want to capture, (have the telephone number available for later use in your script), the capturing expression I posted would make the phone number available via the $matches[1] var.

Jesper Møller
02-13-2010, 08:31 AM
funny tryd that befor asking... but cud not get it to work .. but now it dos ..

May be becaus it 8am and i have been working on this all night :-P

Jesper Møller
02-13-2010, 08:34 AM
Ahhh ... ok

I am going to use it later ... alogn with other entrys like name and adress
Ill look into that $matches[]

MattF
02-13-2010, 08:40 AM
Ahhh ... ok

I am going to use it later ... alogn with other entrys like name and adress
Ill look into that $matches[]

It's a case of either/either, to be honest. The $string var that you're checking against would, (provided it passes the preg_match check), already contain a valid phone number. The $matches var from the preg_match would only come into it's own if you were literally extracting the phone number from a longer string. The fact that you're tying the expression to the start and end of the string with ^ and $ respectively means that you're safe just using $string as your number, once it has passed the validation check.

JAY6390
02-13-2010, 04:13 PM
This is why I love regex buddy
It can explain the whole regex for you. This is the explanation for MattF's regex

Regex info

^\+?([\d\s]+)$

Assert position at the beginning of the string ^
Match the character “+” literally \+?
Between zero and one times, as many times as possible, giving back as needed (greedy) ?
Match the regular expression below and capture its match into backreference number 1 ([\d\s]+)
Match a single character present in the list below [\d\s]+
Between one and unlimited times, as many times as possible, giving back as needed (greedy) +
A single digit 0..9 \d
A whitespace character (spaces, tabs, line breaks, etc.) \s
Assert position at the end of the string (or before the line break at the end of the string, if any) $

Hope that helps explain the whole process for you :)

Jesper Møller
02-13-2010, 06:03 PM
Thanks for all your help.

Learning php is mutch easyer when peapol are so helpsful explaining :-)

Jesper Møller
02-13-2010, 09:12 PM
This is why I love regex buddy
It can explain the whole regex for you.

Sadly regex buddy aint for mac then :(

Maby i can find somthing like it for my mac

JAY6390
02-13-2010, 11:31 PM
Nope it's not free either, but it's a damn good tool

Jesper Møller
02-14-2010, 12:08 AM
Nope it's not free either, but it's a damn good tool

Well not many god things ar free :)

I dit a little google for a mac substitute
found a few here
http://osx.iusethis.com/search?q=regex



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum