how to parse $_REQUEST? - php

here i am checking for and getting part of the request, (the ids). i also print out the entire request:
if (isset($_REQUEST['ids'])){
$amount = sizeof($_REQUEST['ids']);
print_r($_REQUEST);
echo "<br>$amount invitations Successfully Sent";
}
this is how the entire $_REQUEST prints out:
Array
(
[mfs_typeahead_req_form_4cf74f96db3688507476560] => Start Typing a Name
[ids] => Array
(
[0] => 510149460
)
[fbs_258181898823] => \"access_token=258181898823|2.q_qb_yoReO0_xc4H8PxKRQ__.3600.1291280400-100000664203700|LqtGr_OiJTASGmek61awxmxfvFk&expires=1291280400&secret=85eTEELZj8lkV82V_PwRSA__&session_key=2.q_qb_yoReO0_xc4H8PxKRQ__.3600.1291280400-100000664203700&sig=d4cc0e4c0992ea29c0adfd60dc27185b&uid=100000664203700\"
)
i need to parse the part at the end: &uid=100000664203700, specifically '100000664203700'

$queryString = $_REQUEST["fbs_258181898823"];
$output = array();
parse_str($queryString, $output);
print $output["uid"];

parse_str() will break up things that look like a query string. I recommend you pass an array as the second parameter.

You can use parse_str()—it should do the trick.

First, remove the \" (using a combination of preg_replace and stripslashes) at the beginning and the end, then use parse_str:
$str = preg_replace('/(^"|"$)/', '', stripslashes($_REQUEST['fbs_258181898823']));
parse_str($str, $data);
$uid = $data['uid'];

Related

How to extract only parameter value from URL using ONLY Regular Expressions

Extract the value of the u2 parameter from this URL using a regular expression. http://www.example.com?u1=US&u2=HA853&u3=HPA
<?php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
?>
Output:-
u2=HA853
How can i retrieve only HA853?
The 0 group is everything that the regex matched so either use \K to ignore the previous matches of the regex,
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=\K[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
or use a second capture group:
...
$pattern='/u2=([0-9A-Za-z]*)/'; //R.E that url value is only digit/Alphabet
...
print_r($match[1]);
Why you'd need to do that though is unclear to me, http://php.net/manual/en/function.parse-str.php, seems like a simpler approach.
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
parse_str($subject, $output);
echo $output['u2'];
Demo: https://3v4l.org/gR4cb
Other way is to use parse_url,http://php.net/manual/en/function.parse-url.php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
$query_string = parse_url($subject, PHP_URL_QUERY); // get query string
$parameters = explode('&', $query_string); //Explode with &
$array = array(); // define an empty array
foreach($parameters as $val)
{
$param= explode('=', $val);
$array[$param[0]] = $param[1];
}
echo $array['u2']; // outputs HA853
print_r($array);
Array
(
[u1] => US
[u2] => HA853
[u3] => HPA
)

php how to get a string in a specific place

I have this string
http://myipaddress:myport/mycompanyname/morethings?lovelyparameter
I want to take the word mycompanyname
any help?
I tried this:
$indexName = preg_match("http://p+:p+/","http://myipaddress:myport/mycompanyname/morethings?lovelyparameter" );
but I got this error:
preg_match(): Delimiter must not be alphanumeric or backslash
In case you don't want the preg functions, and something else from the url, you can use parse_url(). It would look like this:
$a = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
$b = parse_url($a);
print_r($b);
Output:
Array
(
[scheme] => http
[host] => myipaddress
[port] => 8080
[path] => /mycompanyname/morethings
[query] => lovelyparameter
)
That way, just use something like:
$path = $b['path'];
$foo = explode('/', $path)[1];
echo $foo;
Output:
mycompanyname
Side notes:
This code won't check for malformed url, so you should do some check of your own.
If you test the url with a port number as string (as you have in the question), it won't work.
It could be done in one line:
$url = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
echo explode('/', parse_url($url, PHP_URL_PATH))[1];
Output:
mycompanyname
You can use explode as
$abc = 'http://myipaddress:myport/mycompanyname/morethings?lovelyparameter';
$a = explode('/', $abc);
echo '<pre>';
print_r($a[3]);
echo '</pre>';
The explode breaks the strings into parts and returns an array of strings so you can check in array too for mycompanyname..
For the records, you were missing appropriate delimiters. A regex solution would be:
https?://.+?/(?P<company>[^/]+)/
In PHP this would be:
$regex = '~https?://.+?/(?P<company>[^/]+)/~';
$url = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
preg_match($regex, $url, $match);
echo $match["company"];
// mycompanyname

Declaring variable inside string

I have this kind of string.
'"asdfasdf","123456", this is a message. OK'
What i want to do is declare variables according to the first, second quotation and the rest of the message until the OK...
(note: the length of the string inside the '' is not consistent)
$First = "asdfasdf"
$Second = "123456"
$Message = "this is a message"
is this even possible?
is there something like " "$First","$Second", "$Message" OK " kind of way?
TIA.
Is this a CSV file ?
Doesn't seem to, but if it was you should check out the csv functions of php, specifically str_getcsv.
If not, you should just do an explode by , or ", or any combination you think would be most accurate, and then go through each array item.
$string = '"asdfasdf","123456","this is a message. OK"';
$temp = explode('","',$string);
$array = array();
foreach($temp as $key=>$value){
//do stuff with $value and $key
}
You can use regular expressions, like this:
Code
$raw = '"asdfasdf","123456", this is a message. OK'; // this is your raw text
preg_match('/^"(?P<first>[^"]+)","(?P<second>[^"]+)",\s+(?P<message>.+?) OK/', $raw, $matches); // this looks for the pattern you defined and stores the matches in $matches
print_r($matches); // this just dumps out the array of matching substrings
Output
Array
(
[0] => "asdfasdf","123456", this is a message. OK
[first] => asdfasdf
[1] => asdfasdf
[second] => 123456
[2] => 123456
[message] => this is a message.
[3] => this is a message.
)
You can access the individual substrings as, for example, $matches['first'], $matches['second'], or $matches['message'].
PHP Demo
Regex Demo

convert url string to array php

I pass with jquery ajax as data array the following structure
$_POST['data'][0] = 'results[]=stein&results[]=schere&results[]=stein&results[]=schere&results[]=stein'
$_POST['data'][1] = '9b2c1230757e4354b384c5c93e8e8f26'
How do I say to php to interpret $_POST['data'][0] as array. What I would like to get is array(1 =>'stein', 2=>'schere'...)
Use parse_str() — Parses the string into variables
$str = "results[]=stein&results[]=schere&results[]=stein&results[]=schere&results[]=stein";
parse_str($str, $output);
echo $output['results'][0]; // stein
Live CodePad
use parse_str()..
parse_str($_POST['data'][0]);
print_r($results);
echo $results[0]; //stein;
echo $results[1]; //schere;

Problems with PHP explode

I'm using this code:
$imageurl = "http://siteadress/sites/default/files/bjorn_4.jpg";
$pieces = explode('/', $imageurl);
print_r($pieces);
to split up an url.
The print_r gives me this result =
Array ( [0] => http://siteadress/sites/default/files/bjorn_4.jpg )
Shouldn't it split up the URL after each /? So it will be Array ( [0] => http:/ [1] => / [2] => siteadresses or something like that?
I think you should try :
$imageurl = [node:field_banner_image];
Because with the quotes explode will think that the string is [node:field_banner_image] and not the string inside.
like Edouard Moinard said
$imageurl = [node:field_banner_image];
$pieces = explode('/', $imageurl);
print_r($pieces);
this should work
Try to save your Array[0] element to any variable and split that variable like:
$image=Array[0];
$pieces = explode('/', $image);
print_r($pieces);
I'm unfamiliar with Drupal but a quick read of the documentation gave me this:
http://api.drupal.org/api/drupal/core!includes!token.inc/function/token_replace/8
token_replace()
Replaces all tokens in a given string with appropriate values.
Hopefully that helps
Just giving a try
Check the view source of the page, check if you are getting %2F or "/"
Secondly, check with explode('/', << the field value >>)

Categories