在PHP中将包含数字范围的GET变量转换为字符串
I have a get variable in this format : 0-1499
. Now I need to convert it to a string so that I can explode the variable. For this I tried to convert it to string , but I am not getting any output. Here is the sample code :
$mystring = $_GET['myvars']; //equals to 0-1499;
//$mystring = (string)$mystring;
$mystring = strval($mystring);
$mystring = explode("-",$mystring);
print_r($mystring);
The above print_r()
shows an array Array ( [0] => [1] => 1499 )
. That means it calculates the $mystring
before converted into string. How can I send 0-1499
as whole string to explode
?
我有一个这种格式的get变量: 上面的 0-1499 code>。 现在我需要将其转换为字符串,以便我可以爆炸变量。 为此,我试图将其转换为字符串,但我没有得到任何输出。 以下是示例代码: p>
$ mystring = $ _GET ['myvars']; //等于0-1499;
// $ mystring =(string)$ mystring;
$ mystring = strval($ mystring);
$ mystring = explode(“ - ”,$ mystring);
print_r( $ mystring);
code> pre>
print_r() code>显示了一个数组
Array([0] => [1] => 1499) code>。 这意味着它在转换为字符串之前计算
$ mystring code>。 如何将
0-1499 code>作为整个字符串发送到
explode code>? p>
div>
I have a get variable in this format : 0-1499
When you grab this variable from the URL say.. http://someurl.com/id=0-1499
$var = $_GET['id'];
This will be eventually converted to a string and you don't need to worry about it.
Illustration
FYI : The above illustration used the code which you provided in the question. I didn't code anything extra.
You need quotes, sir.
Should work fine like this.
$mystring = "0-1499";
$mystring = explode("-",$mystring);
print_r($mystring);
Without the quotes it was numbers / math.
0 minus 1499 = negative 1499
Explode is used for strings.http://php.net/explode
<?php
$mystring = "0-1499";
$a=explode("-",$mystring);
echo $a[0];
echo "<br>";
echo $a[1];
?>
see it working here http://3v4l.org/DEstD
As you correctly note it treats the value as arithmetic and ignores the 0-
part. If you know that the value you'll get is 0-n
for some n
, all you need to do is this:
$mystring="0-".$n;
$mystring=explode("0-", $mystring);
but explode
here is a bit redundant. So,
$myarr=array();
$myarr[1]=strval($mystring);
$myarr[0]="0";
There you go.