Hello. I have simpe 2 questions (simple 4 u guys) 1. In the form I have a field 'phone' where I am using mask (xxx)xxx-xxxx. Then I am sending this to php file to $phone. How to get only digits in $phone? 2. How to check in php file if data form field $topic is not empty. And if it is empty how to stop the script .
Thx for help.
dagon;10918773 wrote:1. preg_replace()
1. preg_replace()
is this correct? (xxx)xxx-xxxx
$phone = preg_replace("/\(\d\d\d\)\d\d\d\-\d\d\d\d/i","/\d\d\d\d\d\d\d\d\d\d/i",$phone);
A simple way to strip out all non-digits:
$phone = preg_replace('/[^0-9]/', '', $phone);
Thx.
Could u (or somebody)tell me why in this code
$phonenumber = preg_replace('/[^0-9]/', '', $phone); if ($phonenumber = "1"){ echo "is1"; } if ($phonenumber = "2"){ echo "is2"; }
in result I get
is1is2
when $phonenumber was 2.
= should be ==
...thx....but how to stop script in this case? Is in PHP any function like stop_script() 🙂? This will be my last question this week🙂
you probably want something like this:
if($phonenumber == "1"){ echo "is1"; }elseif($phonenumber == "2"){ echo "is2"; }else{ echo "Not 1 or 2"; }
as for stop_script() that would be exit()
Thank You !! . This example opens new ways for me🙂
Also take a look at the switch construct if you expect to have many different cases.