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 ?
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
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.
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
Related
this is my first post. sorry if i did something wrong...
anyways i have a file that gets updates by php and this is EXACTLY it:
31\n
127\n
131\n
124\n
144\n
142\n
133\n
133\n
9\n
0\n
22\n
18\n
i made this script in php:
$logContents = file_get_contents("logs/mainlog.txt");
$logitemArray = explode("\n", $logContents);
echo max($logitemArray);
but it echos 9. why? it said in the php documentation that max() should return the biggest value in the array
thanks in advance
explode() returns an array of strings, so they're being compared lexicographically. You need to convert them to numbers so that max() will compare them numerically.
$logitemArray = array_map('intval', explode("\n", $logContents));
echo max($logitemArray);
BTW, you can use the file() function to read a file directly into an array of lines, instead of using file_get_contents() followed by explode().
$logitemArray = array_map('intval', file("logs/mainlog.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
Like the comments have said, it's because 9 is the largest lexigraphical value. If it said 900 it would still be the same.
This is because when you split the string with explode you get an array of type string. The following code will convert the elements in the array to integers which should give expected behaviour.
$logitemArray = array_map('intval', explode("\n", $logContents));
I want remove unnecessary text from my string variable. I have variable like $from which contains value like 123456#blog.com. I want only 123456 from it. I have checked some example for trim but does not getting proper idea for do it. Let me know if someone can help me for do it. Thanks
You can split the string on the # symbol like so:
$str = "123456#blog.com";
// here you split your string into pieces before and after #
$pieces = explode("#",$str);
// here you echo your first piece
echo $pieces['0'];
Demo
You can use explode() for splitting the string.
Syntax:
explode('separator', string);
$result= explode("#", '123456#blog.com');
print_r($result);
Output:
Array
(
[0] => 123456
[1] => blog.com
)
I want to extract the dimension from this given string.
$str = "enough for hitting practice. The dimension is 20'X10' *where";
I expect 20'X10' as the result.
I tried with the following code to get the number before and after the string 'X. But it is returning an empty array.
$regexForMinimumPattern ='/((?:\w+\W*){0,1})\'X\b((?:\W*\w+){0,1})/i';
preg_match_all ($regexForMinimumPattern, $str, $minimumPatternMatches);
print_r($minimumPatternMatches);
Can anyone please help me to fix this? Thanks in advance.
Just remove the \b from your pattern (and append a \' in the end if you want the trailing quote):
$regexForMinimumPattern ='/((?:\w+\W*){0,1})\'X((?:\W*\w+){0,1})\'/i';
NB: \b is the meta-character for word-boundaries, you don't need it here.
Assuming that the format of the string we want is 00'X00 :
$regexForMinimumPattern ='/[0-9]{1,2}\'X[0-9]{1,2}/i';
this gives you a result like
Array ( [0] => Array ( [0] => 20'X10 ) )
So: can a simple preg_replace()do that? Perhaps...
<?php
$str = "enough for hitting practice. The dimension is 20'X10' *where";
$dim = preg_replace("#(.*?)(\d*?)(\.\d*)?(')(X)(\d*?)(\.\d*)?(')(.+)#i","$2$3$4$5$6$7", $str);
var_dump($dim); //<== YIELDS::: string '20'X10' (length=6)
You may try it out Here.
you can do numeric index in string like in array.
ex.
$text = "esenihc gnikcuf yloh";
echo $text[0];
echo $text[1];
echo $text[2];
...................
...................
...................
But if you put string in print_r() not same will happen like in array and you cant do count() with string.
I read the documentation and it says.
count()
return 1 if not an array in the parameter
print_r()
if string is in parameter it just prints that string.
this is not the exact word but something like this.
Why both these functions dont treat string same as an array?
So final question is string an array?
Unlike for example C, PHP has an inbuilt string datatype. The string datatype allows you array-like access to the single characters in the string but will always be a string. So if you pass it to a function that accepts the mixeddatatype this function will determine the datatype of the passed argument and treat it that way. That is way print_r() will print it in the way it was programmed to output strings and not like an array.
If you want a function that works does the same as count for arrays have a look at strlen.
If you want you can "turn" your string into an array through str_split.
A string is an array if you treat it as an array, eg: echo $text[0], but print_r Prints human-readable information about a variable, so it will output that variable.
It's called Type Juggling
$a = 'car'; // $a is a string
$a[0] = 'b'; // $a is still a string
echo $a; // bar
To count a string's length use strlen($string) then you can for a for()
no a string is no array
A string is series of characters, where a character is the same as a byte and An array in PHP is actually an ordered map. A map is a type that associates values to keys.
simply everything in the sense every variable in PHP is an array.
Maybe too late but:
<?php
$text = "esenihc gnikcuf yloh";
$arrText = explode(" ", $text);
foreach($arrText as $word) {
echo $word . "<br>";
}
?>
I'm having trouble with PHP text parsing
I have a txt file which has this kind of information:
sometext::sometext.0 = INTEGER: 254
What i need is to get the last value of 254 as variable in PHP.
in this txt file this last value can change from 0 to 255
"sometext::sometext.0 = INTEGER: " this part doesn't change at all.
It has a length of 36 symbols, so i need get with PHP what is after 36 symbol into variable.
Thank you.
Try using sscanf:
$string = "sometext::sometext.0 = INTEGER: 254";
sscanf($string, "sometext::sometext.0 = INTEGER: %d", $number);
echo $number;
Demo: http://codepad.org/Ash2QHvI
Perhaps substr() will do?
$text = "sometext::sometext.0 = INTEGER: 254";
print substr($text, 37);
See it in action here (adjusted to match your sample data): http://codepad.org/5Ikt3kRh
Try fgets for reading a file.
For the parsing I use split. I wrote an example here. But sscanf seems to be the better option.
Since you know the string is always going to be of that form you can use the substr() function to extract the part of the string you want. Then use intval() to make it an integer if needed.
Note: My count shows that the number you're trying to get starts at the 33rd position (which becomes 32 below, as substr is 0 based), not the 36th.
You can get what you want with:
$the_str = 'sometext::sometext.0 = INTEGER: 254';
$num = intval(substr($the_str, 32));