PHP How to parse a string and conver to variables - php

Using PHP, I am looking to parse a string and convert it into variables. Specifically, the string will be a file name, and I'm looking to break the file name into variables. Files are nature photos, and have a uniform format:
species_name-date-locationcode-city
All files names have this exact format, and the "-" is the delimiter. (I can make it whatever if necessary)
an example of the file name is common_daisy-20130731-ABCD-Dallas
I want to break it up into variables for $speciesname, $date, $locationcode, $city.
Any idea on how this can be done? I have seen functions for parsing strings, but they usually take the form of having the name of the variable in the string, (For example "species=daisy&date=20130731&location=ABCD&city=Dallas") which I don't have, and cannot make my file names match that. If I wanted to use a string replace to change the delimiters to variables= I would have to use 4 different delimiters in the filename and that wont work for me.
Thanks for anyone who tries to help me with this issue.

list($speciesname, $date, $locationcode, $city) = explode('-', $filename);

Use PHP explode
$pieces = explode('-', 'species_name-date-locationcode-city');
$name = $pieces[0];
$data = $pieces[1];
$code = $pieces[2];
$city = $pieces[3];

<?php
$myvar = 'common_daisy-20130731-ABCD-Dallas';
$tab = explode ('-', $myvar);
$speciesname = $tab[0];
$date = $tab[1];
$locationcode = $tab[2];
$city = $tab[3];
?>

You can explode the string on delimiter and then assign to variables
$filename = 'species_name-date-locationcode-city';
$array = explode('-', $filename);
$speciesname = $array[0];
$date = $array[1];
$locationcode = $array[2];
$city = $array[3];

http://php.net/manual/en/function.explode.php
$pieces = explode("-", $description);

Related

Convert URL GET var to another variable

I have a URL variable that looks like this:
http://myURL?price_rang=2%5E%5E99_5%5E%5E99
With php echo on the price_rang GET var it translates to 25E5E99_55E5E99 .
I need it to be converted to 2.99_5.99 and then explode it and use the first and second part in a query.
What would be the best way to convert this string and explode it (or just convert it)?
Take it step by step.
get the info
replace your string
explode
Try the following:
$range = $_GET['price_rang'];
$range = str_replace("^^", ".", $range);
$val = explode("_", $range);
var_dump($val);
You need rawurldecode() or urldecode() function
$val = explode('_', rawurldecode($_GET['price_rang']));

Parameters from URL separate

i have got an URL:
http://website.example/script.php?timestamp=2014-10-31T16%3A12%3A57&sms=PM+000+name
and i want to catch values separated by '+'
if there is ability to add to array or what:
array sms[
array_value1 = PM
array_value2 = 000
array_value3 = name]
and then take value like
$name1 = $array_value3;
which will be "name"
Also if it would be done from this:
$timestamp = "2014-10-31T16%3A12%3A57";
this:
$timestamp = "2014-10-31-16:12:37";
Thanks for reply.
$foo = explode(' ', $_GET['sms']); // array('PM','000','name')
That what you're after? The + is just an encoding for a space (%20), so server-side you're looking at splitting it by spaces (though they would all fall under the same parameter).
Keep in mind if "name" turned into "name surname", you'll end up with a 4th array element containing "surname". (Array('PM','000','name','surname'))
$url=urldecode("http://website.example/script.php?timestamp=2014-10-31T16%3A12%3A57&sms=PM+0+name");
$url=parse_url($url, PHP_URL_QUERY);
$url_split = explode('&', $url);
$timestamp=$url_split[0];
$sms=explode(' ', $url_split[1]);
Close but i wanted right this:
$raw = $_GET["sms"];
list($type, $number, $user) = explode(" ", $raw, 3);
and echo $user would give result: name
Now need to do somethink with that time formatting.

Getting PHP variables from .txt file and discarding characters

I know the pure basics of PHP, and I need help converting text into variables from a .txt file.
The text inside the .txt file (lets call it 'info.txt') is in a single line as follows:
Robert | 21 | male | japanesse |
So what I need is to convert the information in variables as follows:
<?php
$name = 'Robert';
$age = '21';
$sex = 'male';
$nacionality = 'japanesse';
?>
Note that I want to discard the '|' between every data.
How could I do that using PHP? Using arrays? How?
<?php
$file_content = file_get_contents($fileName);
list($name, $age, $sex, $nationality) = explode("|", $file_content);
echo "Hello ". $name;
Use explode to get information in an array.
You can use php's file_get_contents() & explode() functions
$data = file_get_contents('info.txt');
$parsedData = explode("|", $data);
var_dump($parsedData);
You can "explode" a string in PHP using the explode function. You can also use file_get_contents to get the contents of a file. Assuming that the format of the file is always consistent, you can couple explode with list to assign directly to your variables.
For example
<?php
$string = file_get_contents("file.txt");
$lines = explode("\n", $string);
list($name, $age, $sex, $nationality) = explode("|", $lines[0]);
This reads the contents of "file.txt" into an array, and then assigns the first line's contents to the variables $name, $age, $sex, $nationality
Code
//Step 1
$content = file_get_contents('info.txt');
//Step 2
$info = explode('|', $content);
//Step 3
$name = $info[0];
$age = $info[1];
$sex = $info[2];
$nationality = $info[3];
Explaination
First load the contents in info.txt in a variable using the
file_get_contents() function:
$content = file_get_contents('info.txt');
Second, break up the content into little pieces based on the | character using the explode() function. The broken bits will be stored in an array.
$info = explode('|', $content);
Now assign each value in the array from step 2 to a variable
$name = $info[0];
$age = $info[1];
$sex = $info[2];
$nationality = $info[3];
you can do this step in a shorter way using the list() function as shown in the other answers!
Ultra short, one line code for fun
list($name, $age, $sex, $nationality) = explode("|", file_get_contents("file.txt"));

php extract value from string

I have a session variable that contains the following string.
a:2:{s:7:"LoginId";s:32:"361aaeebef992bd8b57cbf390dcb3e8d";s:8:"Username";s:6:"aaaaaa";}
I want to extract the value of username "aaaaaa". can anyone suggest easier/more efficient manner?
$session_data = unserialize($_SESSION["SecurityAccess_CustomerAccess"]);
$session_user_array = explode(';', $_SESSION["SecurityAccess_CustomerAccess"]);
$temp = $session_user_array[3];
$temp = explode(':', $temp);
$username = $temp[2];
echo $username;
It just got uglier... had to removed quotes.
if ($_SESSION["SecurityAccess_CustomerAccess"]){
$session_data = unserialize($_SESSION["SecurityAccess_CustomerAccess"]);
$session_user_array = explode(';', $_SESSION["SecurityAccess_CustomerAccess"]);
$temp = $session_user_array[3];
$temp = explode(':', $temp);
$temp = str_replace("\"", "", $temp[2]);
$username = $temp;
echo $username ;
}
This is all you need:
$session_data = unserialize($_SESSION["SecurityAccess_CustomerAccess"]);
echo $session_data['Username'];
Your session data is an array stored in serialized form, so unserializing it turns it back into a regular PHP array.
If the data will always be formed in a similar manner, I would suggest using a regular expression.
preg_match('/"Username";s:\d+:"(\w*)"/',$session_data,$matches);
echo $matches[1];
You could probably write a some type of regular expression to help with some of this if the placement of these values were always the same. But I think what you have here is great for readability. If anyone came after you they would have a good idea of what you are trying to achieve. A regular expression for a string like this would probably not have the same effect.

PHP - get certain word from string

If i have a string like this:
$myString = "input/name/something";
How can i get the name to be echoed? Every string looks like that except that name and something could be different.
so the only thing you know is that :
it starts after input
it separated with forward slashes.
>
$strArray = explode('/',$myString);
$name = $strArray[1];
$something = $strArray[2];
Try this:
$parts = explode('/', $myString);
echo $parts[1];
This will split your string at the slashes and return an array of the parts.
Part 1 is the name.
If you only need "name"
list(, $name, ) = explode('/', $myString);
echo "name is '$name'";
If you want all, then
list($input, $name, $something) = explode('/', $myString);
use the function explode('/') to get an array of array('input', 'name', 'something'). I'm not sure if you mean you have to detect which element is the one you want, but if it's just the second of three, then use that.

Categories