View Full Version : Resolved ereg_replace works preg_replace dont work?

Jesper Møller
02-13-2010, 08:17 PM
I have the following line taht works:

$phonecheck = ereg_replace("[ \+]", "", $phone);

but if i try to use preg_replace insted it dont work.

$phonecheck = preg_replace("[ \+]", "", $phone);

why ?? what do i do wrong ??

02-13-2010, 08:27 PM
works for me, though.

02-13-2010, 08:29 PM
$phonecheck = preg_replace('#[ \+]#', '', $phone);

Jesper Møller
02-13-2010, 08:45 PM
Thanks Matt
thatt got me on track :-)

looks like both #[ \+]#,/[\+\sa-z]/ and ([\+\s]) works :-)

02-13-2010, 08:59 PM
The simplest expression would be:


Anything which isn't a number will be ditched.

Jesper Møller
02-13-2010, 09:08 PM
Nice :-)

Thought that ^ only was finding stuf the beginning of a string
wow i got a lot to learn :-P

Thanks a lot !

Maby you can help me out with the next step ??

I want to check if ther is minimum 8 charecters in the line

This works:

$phonecheck = preg_replace("#[^\d]#","",$phone);
if (strlen($phonecheck) < 8) {
$error = true; }
but this dont ?:

if (strlen(preg_replace("#[^\d]#","",$phone) < 8)) {
$error = true; }

02-13-2010, 09:18 PM
how about \D instead of [^\d], pretty much the same.

02-13-2010, 09:21 PM
$phone isn't set.

Thought that ^ only was finding stuf the beginning of a string

When at the start of a character class, it negates the class. i.e: anything not in the class matches.

02-13-2010, 09:27 PM
how about \D instead of [^\d], pretty much the same.

Good point. :)

Jesper, you may find this link useful for general reference:


Jesper Møller
02-13-2010, 11:39 PM
Funny ... i was actualy goin to ask abuth \d that :-)

i have been using http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html#6 to try to learn this and that said tha \d is Matches any numeric character and \D is Matches any non-numeric character
but the ^ has been a litle kunfusing for me .. but as i see it if ^ is outside ^[] it meens "at the start" and if its inside [^] it meens "is not" ??

Thanks for the link its now a bookmark , it looks a little more idept than the one i was looking at :-) (And a little more confusing) ..

I can see i have a lot to learn

Jesper Møller
02-14-2010, 12:01 AM
$phone isn't set.

Yes it is (youst not shown in my first message)

I have tryde both version in the same and no matter what i try check 1 comes out as if ther is 8 or more number

$phone = '+1234 567';

// Check 1
$phonecheck1 = preg_replace("#[\D]#","", $phone);
echo 'number: '.$phonecheck1.'<br>';

if (strlen(preg_replace("#[\D]#","",$phone) < 8)) {
echo 'Check1 = Is NOT 8 or more'; }
echo 'Check1 = Is 8 or more'; }

echo '<br>';

// Check 1
$phonecheck2 = preg_replace("#[\D]#","",$phone);
if (strlen($phonecheck2) < 8) {
echo 'Check2 = Is NOT 8 or more'; }
else {
echo 'Check2 = Is 8 or more'; }

Jesper Møller
02-15-2010, 05:06 AM
Well my original question was answered, So i will mark it as resolved


The part of the working and not working code must be a new questio ? so i have made a new tread for it http://www.codingforums.com/showthread.php?t=189248

Thanks for the help, you guys rock :thumbsup:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum