I need away to tell if a $_POST[phone] is starting with 04 if it is I want run a query
In javascript I know how to do this but for some reason on Ubuntu 8 (Yes I know its old) and Firefox the agents are able to still post 04 number in mobile field after testing my javascript query over and over again - I thought instead
You can do a substr like this:
if (substr($_POST['phone'], 0, 2) == '04') {
// ... do some code here
}
If you want to match multiple strings at the same time you could do this (it's faster than doing an in_array()):
$prefixes = array('02'=>true, '03'=>true, '07'=>true, '08'=>true);
$inputPrefix = substr($_POST['phone'], 0, 2);
if (isset($prefixes[$inputPrefix])) {
// ... do some code here
}
Based on your comment if you want to make sure it is only 03, 02, 07 or 08 you can do the following:
$telephonePrefix = substr($_POST['phone'], 0, 2);
$knowPrefixes = array('03','02','07','08');
if (in_array($telephonePrefix , $knowPrefixes)) {
// ... do some code here
}
Related
So I've created a function to allow letters single letters to be changed in to a 2 digit number.
This is the code:
<?php
function Letter2number($abc) {
//A = 00
str_replace('a','00',$abc);
str_replace('A','00',$abc);
//B = 01
str_replace('b','01',$abc);
str_replace('B','01',$abc);
//C = 02
str_replace('c','02',$abc);
str_replace('C','02',$abc);
//D = 03
str_replace('d','03',$abc);
str_replace('D','03',$abc);
//E = 04
str_replace('e','04',$abc);
str_replace('E','04',$abc);
//F = 05
str_replace('f','05',$abc);
str_replace('F','05',$abc);
}
?>
When the variable from a form input is processed though this function it was meant to output a 2 digit number.
But the output is blank.
Any idea what I'm doing wrong?
First attempt at programming? I hope you have a bit of fun. There are two major problems with your code, both have to do with return values.
Something like str_replace('F','05',$abc); returns a string, which you have to do something with. For instance: $abc = str_replace('F','05',$abc);.
Your function doesn't return anything. At the bottom of the function use return $abc;.
Also consider your choice of variable name: $abc implies that it contains the alfabet, not one letter. A better name would be $letter.
TIP: I used $abc in the above points to not confuse you any further, but the code itself is far from optimal. Can you make it better?
Let say currentmonth is 06 and currentyear 2017
i have dropdown of year and month ..
if i select year 2017 and month of July which is larger then currentmonth
how do i show there is not data available yet
my current code..and its not working..i'm a newbie and not sure im doing this right or not...
$mystartingyear=2017;
$mystartingmonth=01;
$currentmonth=date("m");
$currentyear=date("Y");
if ($currentyear >= $mystartingyear && $currentmonth >= $mystartingmonth)
{
echo "Show Data";
}
else
{
echo "No Data yet";
}
i also tried it like this
$mystartingyear=2017;
$mystartingmonth='01';
$currentmonth=12;
$currentyear=2017;
//$currentmonth=date("m");
//$currentyear=date("Y");
if ($currentyear >= $mystartingyear && $currentmonth >= $mystartingmonth)
{
echo "Show Data";
}
else
{
echo "No Data yet";
}
it always display "show data"
Edit:
Integers from 01 to 07 are ok just as long as you don't do 010 (with 3 integers) since that will be represented as 8. But as soon as you start hitting 08 and 09 for the months of August and September (which may be what's in your unknown dropdown), you will have problems.
So it's best you quote it.
Consult "Footnotes".
Original answer:
The leading zero in 01 for:
$mystartingmonth = 01;
^^
is treated as an octal.
It needs to be quoted:
$mystartingmonth = '01';
Octals references:
http://php.net/manual/en/language.types.integer.php
https://en.wikipedia.org/wiki/Octal
Footnotes:
If your current code is failing you, then something else is failing you.
You mention in your question that you're using this from a dropdown and that code wasn't included, nor how it's being passed.
Use PHP's error reporting, set to catch and display:
http://php.net/manual/en/function.error-reporting.php
Verify your conditional statement's logic also; both conditions must be met.
There is missing semicolon after first echo i.e. code should be
echo "Show Data";
Then every thing should work fine,
The date(); function in php returns the date in string and when you compare a string with integer it is implicitly converted into equivalent integer variable,if you want for sure you can use intvar(); function which converts variable to equivalent integer.But in your case it is not necessary.For more about i recommend you to read php manual .
I have a primary key field (news_id) in the news table
it start from 1, 2, 3, 4 and so on..
However, I like to change to 01, 02, 03, 04,e tc ... is that possible?
If not, how can that be done in PHP?
Manipulating the keys directly is a bad idea in 99% of cases.
The best way to go is probably to change the format when outputting the keys like shown in this question:
$key = 4;
echo sprintf('%02d', $key); // outputs 04
If it's for output, just add a 0 if the primary key is less than 10:
if($result['id'] < 10){
echo '0' . $result['id'];
}
Okay... Another way of doing this is... (though overly logical and mathematical...)
$id = 5;
$noOfZeros = 2; // i.e. you are converting 2 to 002.
$divider = pow(10,$noOfZeros); // Now you are creating a divider (/100 in this case)
$id = $id / $divider; // dividing the id by 100. i.e. 5 gets converted to 0.05
$zeroedId = str_replace(".","",$id); // finally replace the "." with nothing... so 0.05 becomes 005 :).
In PHP what is the quickest way to convert an mobile number to international format:
So 07123456789 becomes 447123456789.
I have tried a few ways and cant seem to get it to work.
This is current script:
if(strlen($gsm) > 2) {
if(!substr_compare($gsm, "07", 0, 2, false)) {
unset($gsm);
}
elseif (substr_compare($gsm, "07", 0, 3, true)) {
if(strlen($gsm) == 11) {
return "447" . substr($gsm, 2);
}
}
}
Note: This script only runs if the number matches a regex.
Something like this, perhaps?
$intl_number = preg_replace('/^0/','44',$uk_number);
or if you specifically only want to do UK mobile numbers:
$intl_number = preg_replace('/^07/','447',$uk_mob_number);
(note: I'm assuming UK-specific since you specified '44' in the question)
This does use Regex, but should be pretty quick in execution speed since it is anchored to the begining of the string.
Fastest way that comes to mind for me, as long as you are shre the number matches the format 07xxxxxx at this point:
$number = "07123456789";
$number = '44'.substr($number,1);
There is a PEAR package for validating international telephone numbers.
Hay how can I convert
01 to 1
02 to 2
all the way to 9?
Thanks
I assume the input is a string?
$str = "01";
$anInt = intval($str);
You may think that the leading 0 would mean this is interpreted as octal, as in many other languages/APIs. However the second argument to intval is a base. The default value for this is 10. This means 09->9. See the first comment at the intval page, which states that the base deduction you might expect only happens if you pass 0 in as the base.
$x="01";
$x=+$x;
$x="02";
$x=+$x;
...
or
$x=+"01";
should work for both int, and string
Do (int)$str; instead. It's up to 4x faster than intval().
$str = "05";
$last = $str[1];
$i = substr($input, 1, 1);
If you want to use the generic regular expression solution: 's/[0]([0-9])/\1/' (add in anchors as appropriate)
$str='05';
if(strpos($str,'0')==0 && strpos($str,'0') != false ){
$new = intval(substr($str,1));
}
you can use the predefined php function .
intval()
For Ex:
$convert = intval(01);
echo $convert ;
It will print 1(one);