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));
Related
I have a string: /userPosts/hemlata993/20
I want to remove this /userPosts/hemlata993/.
I checked some answers but not being able to remove the first part. How can I do that? I am using php.
$string = /userPosts/hemlata993/20
I want the output as 20 because 20 is the directory or file name that I want to get
You can do it as follows:
$p = basename(parse_url("/userPosts/hemlata993/20")['path']);
echo $p; //20
If this is what you want and the format of the string is always going to be like the one that you provided, this will work:
$string = "/userPosts/hemlata993/20";
$string_arr = (explode("/",$string));
echo $string_arr[3];
I got this result from mysql query using php:
chris123
I want to separate chris from 123 because I am going to use the value of int for operation.
$char=string('chris123');
$int=int('chris123');
How am I able to do it in PHP? Not PDO. Not so familiar with it. Thanks.
If you know the non-number parts are always going to be lowercase characters, you can do this:
$str = 'chris123';
$num = trim($str, 'a..z');
If you also want it to be an actual integer, you can cast it:
$str = 'chris123';
$num = (int) trim($str, 'a..z');
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
This should be simple to answer, I'm just very new to this all...
If I have the following (the 89 is NOT constant — it could be 2, sometimes 3 numbers — but the /100 is):
89/100
how can I get just 89 saved as an integer?
I Would explode on the /
explode('/','89/100');
then your result would be in the start of the array.
You can just use intval():
$num = intval($string);
This parses the string as an integer, and in this case, ignores everything from the "/" onwards.
To be clear, this will not include the "100".
intval("89/100") => 89
Here's a phpfiddle showing that it works: http://phpfiddle.org/main/code/n1b-reb
You can split the string up by the delimiter /, with explode().
$string = '89/100';
$data = explode('/', $string);
// 89
$number = (int)$data[0];
I need to check a string using PHP for a particular text component and replace the value if it is present with a new value. For example I might have a string like this:
?customerID=12345&recid=65&skip=20&type=job
I need to locate the "&skip=20" (it won't always = 20, it could be any number like 20, 80, 100, etc) substring and replace the "20" value with a new value from a variable ($newValue). So if that string was present and $newValue = 40 the new string would be:
?customerID=12345&recid=65&skip=40&type=job
If the string was:
?customerID=12345&recid=65&skip=160&type=job
the new string would be ($newValue = 180):
?customerID=12345&recid=65&skip=180&type=job
I'm pretty sure I should be using these functions - strpos, preg_match, preg_replace - but I've spent way too long on this and don't appear to be getting any closer. If anyone can show me how to use these or other functions to find the substring and replace the value that would be much appreciated.
Try preg_replace_callback
<?php
$str = "?customerID=12345&recid=65&skip=20&type=job";
$newStr = preg_replace_callback("/skip=([0-9]+)/i", "doIt", $str)
function doIt($match){
return $match[1] + 20;
}