What do I need to do to convert the number only if all the number matches?
At the moment, it’s converting to each number. (I’ve tried to do a lot and I didn’t succeed) I would like to know if have any way of adapting to only show the word if the number “is complete”, otherwise show the word “Doesn’t contain a name record”.
Example: When I try to use the number 1987, the output its: Name1 Name9 Name8 Name7, and I want to be only one word: Name1987.
function numberTowords($num)
{
$ones = array(
0 => "Name0",
1 => "Name1",
2 => "Name2",
3 => "Name3",
4 => "Name4",
5 => "Name5",
6 => "Name6",
7 => "Name7",
8 => "Name8",
9 => "Name9",
10 => "Name10",
11 => "Name11",
12 => "Name12",
13 => "Name13",
14 => "Name14",
15 => "Name15",
16 => "Name16",
17 => "Name17",
18 => "Name18",
19 => "Name19",
1987 => "Name1987",
2398 => "Name2398",
);
$num = number_format($num, 2, ".", ",");
$num_arr = explode(".", $num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",", $wholenum));
krsort($whole_arr, 1);
$rettxt = "";
foreach ($whole_arr as $key => $i) {
while (substr($i, 0, 1) == "0")
$i = substr($i, 1, 5);
if ($i < 20) {
/* echo "getting:".$i; */
$rettxt .= $ones[$i];
} elseif ($i < 100) {
if (substr($i, 1, 1) != "0") $rettxt .= " " . $ones[substr($i, 1, 1)];
} else {
if (substr($i, 0, 1) != "0") $rettxt .= $ones[substr($i, 0, 1)];
if (substr($i, 2, 1) != "0") $rettxt .= " " . $ones[substr($i, 2, 1)];
}
if ($key > 0) {
$rettxt .= " ";
}
}
if ($decnum > 0) {
$rettxt .= " and ";
if ($decnum < 20) {
$rettxt .= $ones[$decnum];
} elseif ($decnum < 100) {
$rettxt .= " " . $ones[substr($decnum, 1, 1)];
}
}
return $rettxt;
}
Advertisement
Answer
only show the word if the number “is complete”, otherwise show the word “Doesn’t contain a name record”.
Example: … number 1987 I want (output) to be only one word: Name1987.
If this is all you need – piece of cake:
<!-- this is only a tool for easy test -->
<form><input name="nr"><input type="submit"></form>
<?php
$pattern = array_merge(range(0,19),[1987,2398]);
$nr = $_GET[nr];
echo in_array($nr,$pattern) ? "Name$nr" : "Doesn't contain a name record";
array_merge() joins arrays, range() is kind of array, in_array() checks if a value exists in an array
more flexible – for cases you omitted in your question
$pattern = [0=>'my name is Zero',
1=>'I am the ONE',
12345=>'this is a number',
'12345'=>'this is a string',
'7x24'=>'all time shift',
999=>'almost thousand',
12=>'call me a dozen'];
echo array_key_exists($nr,$pattern) ? $pattern[$nr] : "Doesn't contain a name record";
// if you don't like shorthand - different notation:
if(array_key_exists($nr,$pattern)){ echo $pattern[$nr];}
else{ echo "Doesn't contain a name record";}
Arrays are fast and nice in use. Built-in functions are optimised and that makes them much better than step-by-step manipulations. Debugging a few lines of code is easy, debugging plenty lines of particular manipulations is a nightmare. For most cases in php the only concern is to find the name of built-in function which do it all at once.
I didn’t want to limit the numbers (…) want to add more than 200 ids
I didn’t limit anything, and it is not that much, even as associative array it wouldn’t be big array
more complex – it’s time to organise the code structure
function name($x,$arr){
if(in_array($x,range(130,150))){ $name = 'from 130 to 150';} //or (($x >= 130) && ($x <= 150)), or ... etc
else if(in_array($x,range(100,200))){ $name = 'from 100 to 200';}
else if(array_key_exists($x,$arr)){ $name = $arr[$x];}
else{ $name = 'pass';}
$z = ($x[0] == 0) ? 'leading zero' : '∗'; //or if-else instead of Elvis, (substr($x,0,1) == 0) ...
return($z.' '.$name);}
echo '<p>'.name($nr,$pattern).'</p>';
In the name()
function, sequence is important for the sub-ranges.
ternary operator a.k.a. Elvis
filter_var() if you like it