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.
Related
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);
I have looked around for this but can only find links and references to this been done after an anchor hashtag but I need to get the value of the URL after the last / sign.
I have seen this used like this:
www.somesite.com/archive/some-post-or-article/53272
the last bit 53272 is a reference to an affiliate ID..
Thanks in advance folks.
PHPs parse_url (which extracts the path from the URL) combined with basename (which returns the last part) will solve this:
var_dump(basename(parse_url('http://www.somesite.com/archive/some-post-or-article/53272', PHP_URL_PATH)));
string(5) "53272"
You can do this :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$id = substr(url, strrpos(url, '/') + 1);
You can do it in one line with explode() and array_pop() :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
echo array_pop(explode('/',$url)); //echoes 53272
<?php
$url = "www.somesite.com/archive/some-post-or-article/53272";
$last = end(explode("/",$url));
echo $last;
?>
Use this.
I'm not an expert in PHP, but I would go for using the split function: http://php.net/manual/en/function.split.php
Use it to split a String representation of your URL with the '/' pattern, and it will return you an array of strings. You will be looking for the last element in the array.
This will work!
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$pieces = explode("/", $url);
$id = $pieces[count($pieces)]; //or $id = $pieces[count($pieces) - 1];
If you always have the id on the same place, and the actual link looks something like
http://www.somesite.com/archive/article-post-id/74355
$link = "http://www.somesite.com/archive/article-post-id/74355";
$string = explode('article-post-id/', $link);
$string[1]; // This is your id of the article :)
Hope it helped :)
$info = parse_url($yourUrl);
$result = '';
if( !empty($info['path']) )
{
$result = end(explode('/', $info['path']));
}
return $result;
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$parse = explode('/',$url);
$count = count($parse);
$yourValue = $parse[$count-1];
That's all.
I have a string like this [tubelist dijfisj, ijdsifjad, ajkdfksd, sdjfkdf] and I would like to separate them into two ###URL### and ###URL2###.
This is the code I got so far
function xyz_plugin_callback($match)
{
$tag_parts = explode(",", rtrim($match[0], "]"));
$output = YOUX_TARGET;
$output = str_replace("###URL###", $tag_parts[1], $output);
$output = str_replace("###URL2###", $tag_parts[2], $output);
}
$match is the variable that I'm passing in.
You can utilize regular expressions here.
So if you do something like:
$temp_match = trim($match[0], "[]");
$urls = array();
preg_match("/([\w\s]*),([\w\s]*),.*/", $temp_match, $urls);
$url1 = $urls[1];
$url2 = $urls[2];
// do your $output str_replace here.
finally, I got it working! Here is the working code.
$tag_parts1 = explode(" ", rtrim($match[0], "]"));
$tag_parts = explode(",",$tag_parts1[1],2);
the first line will strip the [tubelist and ].
the second line with store the first set of value in array[0] and the rest to [1].
voila, case resolved.
I am trying to parse following text in variable...
$str = 3,283,518(10,569 / 2,173)
And i am using following code to get 3,283,518
$arr = explode('(',$str);
$num = str_replace(',','',$arr[0]); //prints 3283518
the above $str is dynamic and sometimes it could be only 3,283,518(means w/o ()) so explode function will throw an error so what is the best way to get this value? thanks.
$str = "3,283,518(10,569 / 2,173)";
preg_match("/[0-9,]+/", $str, $res);
$match = str_replace(",", "", array_pop($res));
print $match;
This will return 3283518, simply by taking the first part of the string $str that only consists of numbers and commas. This would also work for just 3,283,518 or 3,283,518*10,569, etc.
Probably going to need more information from you about how dynamic $str really is but if its just between those values you could probably do the following
if (strpos($str, '(' ) !== false) {
$arr = explode('(',$str);
$num = str_replace(',','',$arr[0]);
} else {
//who knows what you want to do here.
}
If you are really sure about number format, you can try something like:
^([0-9]+,)*([0-9]+)
Use it with preg_match for example.
But if it is not a simple format, you should go with an arithmetic expressions parser.
Analog solution:
<?php
$str = '3,283,518(10,569 / 2,173)';
if (strstr($str, '('))
{
$arr = explode('(',$str);
$num = str_replace(',','',$arr[0]);
}
else
{
$num = str_replace(',','',$str);
}
echo $num;
?>
I have this string:
$guid = 'http://www.test.com/?p=34';
How can I extract the value of get var p (34) from the string and have $guid2 = '34'?
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $vars);
$guid2 = $vars['p'];
If 34 is the only number in the query string, you can also use
echo filter_var('http://www.test.com/?p=34', FILTER_SANITIZE_NUMBER_INT); // 34
This will strip anything not a number from the URL string. However, this will fail the instant there is other numbers in the URL. The solution offered by konforce is the most reliable approach if you want to extract the value of the p param of the query string.
A preg_replace() is probably the quickest way to get that variable, the code below will work if it is always a number. Though konforce's solution is the general way of getting that information from a URL, though it does a lot of work for that particular URL, which is very simple and can be dealt with simply if it unaltering.
$guid = 'http://www.test.com/?p=34';
$guid2 = preg_replace("/^.*[&?;]p=(\d+).*$/", "$1", $guid);
Update
Note that if the URLs can not be guaranteed to have the variable p=<number> in them, then you would need to use match instead, as preg_replace() would end up not matching and returning the whole string.
$guid = 'http://www.test.com/?p=34';
$matches = array();
if (preg_match("/^.*[&?;]p=(\d+).*$/", $guid, $matches)) {
$guid2 = $matches[1];
} else {
$guid2 = false;
}
That is WordPress. On a single post page you can use get_the_ID() function (WP built-in, used in the loop only).
$guid2 = $_GET['p']
For more security:
if(isset($_GET['p']) && $_GET['p'] != ''){
$guid2 = $_GET['p'];
}
else{
$guid2 = '1'; //Home page number
}