Extract all the named parameters from a given url in cakephp - php

I have 1 doubt regarding this...
i have a url like this
http://www.SpiderWeb.com/vendors/search_results/scid:0/atr:1/mbp:1/bc:2/bc:1/mbpo:2/atrt:5/atop:1/opel:4
I want to extract all the named parameters in an array vars such that I have the name of the variable as well it's value in the url so that I can use them for some processing...
Is there some way by which i can achieve this uisng some build-in functions rather than assiging them individually
i.e
$some_var = $this->request->params['named']['id'];
The resaon i want it is because the named parameters are dynamic ....
heres the updated solution for this...
<?php
$url = 'http://www.SpiderWeb.com/vendors/search_results/scid:0/atr:1/mbp:1/bc:2/bct:1/mbpo:2/atrt:5/atop:1/opel:4';
$arr_url = parse_url($url);
//print_r($arr_url);
$res = explode("/vendors/search_results/",$arr_url['path']);
//print_r($res);
//print_r($res[1]);
$vars = explode("/",$res[1]);
//print_r($vars);
$result = array();
foreach($vars as $key => $val){
if (strpos($val, ":") !== false) {
$newvars = explode(":",$val);
//print_r($newvars);
$result[$newvars[0]] = $newvars[1];
}
}
print_r($result);
?>

Just loop through the named params array
foreach ($this->request->params['named'] as $name => $param) {
pr("The param name is {$name}");
pr("The param value is {$param}");
}

Try this :
$url = 'http://www.SpiderWeb.com/vendors/search_results/scid:0/atr:1/mbp:1/bc:2/bc:1/mbpo:2/atrt:5/atop:1/opel:4';
$myarr = parse_url($url);
$res = explode("/",$myarr['path']);
foreach($res as $val){
if (strpos($val, ":") !== false) {
$vars = explode(":",$val);
$$vars[0] = $vars[1];
}
}
echo $scid;

Related

Multidimensional Search in Array By Key Value [PHP]

I am trying to find the url value using itag value in the array.
I want to find the url value in the array with the itag value 18 (itag=18).
PHP Code:
<?php
$videoId = "EJOnwF8mgXc";
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=" .
$videoId), $info);
$streams = $info['url_encoded_fmt_stream_map'];
$streams = explode(',', $streams);
foreach ($streams as $stream) {
parse_str($stream, $data);
print_r($data);
}
A simple "if" statement will do :
<?php
$videoId = "EJOnwF8mgXc";
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=" .
$videoId), $info);
$streams = $info['url_encoded_fmt_stream_map'];
$streams = explode(',', $streams);
foreach ($streams as $stream) {
parse_str($stream, $data);
if($data['itag'] == 18){
echo $data['url'];
}
}

How to remove an object from JSON array in PHP

I have a JSON array. I want to delete the entry that have number 4 and return the left over array
$filters = '{"1":1,"2":2,"3":4}';
$fobj = json_decode($filters, TRUE);
foreach($fobj as $key => $value)
{
if (in_array(4, $fobj)) {
unset($fobj[4]);
}
}
echo $filters = json_encode($fobj );
But this echo does not give me what I want. I want it to return something like this:
{"1":1,"2":2}
You're removing the fourth value of the array, not the value. Use array_search instead
$filters = '{"1":1,"2":2,"3":4}';
$fobj = json_decode($filters, TRUE);
$search = array_search(4, $fobj);
if($search !== false) unset($fobj[$search]);
echo $filters = json_encode($fobj );
$index = array_search("4", $array);
unset($array[$index]);
http://php.net/manual/de/function.array-search.php
http://php.net/manual/de/function.unset.php
That's all. Hope it helps!

PHP renaming string if string already exists

I am storing some data in an array and I want to add the key to it if the title already exists in the array. But for some reason it's not adding the key to the title.
Here's my loop:
$data = [];
foreach ($urls as $key => $url) {
$local = [];
$html = file_get_contents($url);
$crawler = new Crawler($html);
$headers = $crawler->filter('h1.title');
$title = $headers->text();
$lowertitle = strtolower($title);
if (in_array($lowertitle, $local)) {
$lowertitle = $lowertitle.$key;
}
$local = [
'title' => $lowertitle,
];
$data[] = $local;
}
echo "<pre>";
var_dump($data);
echo "</pre>";
You will not find anything here:
foreach ($urls as $key => $url) {
$local = [];
// $local does not change here...
// So here $local is an empty array
if (in_array($lowertitle, $local)) {
$lowertitle = $lowertitle.$key;
}
...
If you want to check if the title already exists in the $data array, you have a few options:
You loop over the whole array or use an array filter function to see if the title exists in $data;
You use the lowercase title as the key for your $data array. That way you can easily check for duplicate values.
I would use the second option or something similar to it.
A simple example:
if (array_key_exists($lowertitle, $data)) {
$lowertitle = $lowertitle.$key;
}
...
$data[$lowertitle] = $local;

Get the names of $_POST variables by value

If I have several post results that are like this:
$_POST["ResponseA"] = 1, $_POST["ResponseB"] = 1, $_POST["ResponseC"] = 2, $_POST["ResponseD"] = 3, $_POST["ResponseE"] = 1, etc.
How can I perform a loop that gets an array based upon the values? So If I'm checking for a value of 1, I get ResponseA, ResponseB, ResponseE ?
<?php
$results = array_keys($_POST, 1);
var_dump($results);
?>
Use array_flip() like this...
$flipped = array_flip($_POST);
echo $flipped['1']; // ResponseA
You will get issues doing this though as your values are not unique
simple build a loop
<?php
$fields = array('ResponseA','ResponseB','ResponseC','ResponseD','ResponseE')
function searchValue(array $fields, $value) {
$out = array();
foreach($fields as $name) {
if(isset($_POST[$name]) && $_POST[$name] == $value) $out[]=$name;
}
return $out;
}
var_dump(searchValue($fields,1));
You can loop through the $_POST:
foreach ($_POST as $key => $value) {
if ($value == 1) {
// $key will equal the value ResponseA if $_POST["ResponseA"] = 1
}
}

Turning off multidimensional arrays via POST in PHP

Is there a way to turn off the PHP functionality for Submitting a multidimensional array via POST with php?
So that submission of <input type="text" name="variable[0][1]" value="..." /> produces a $_POST like so...
array (
["variable[0][1]"] => "...",
)
NOT like so:
array (
["variable"] => array(
[0] => array (
[1] => "..."
),
),
)
I'm thinking/hoping an obscure PHP.ini directive or something... ?
No, but nothing stops you from fetching the query string (through $_SERVER['QUERY_STRING']) and parsing it manually. For instance:
$myGET = array();
foreach (explode("&", $_SERVER['QUERY_STRING']) as $v) {
if (preg_match('/^([^=])+(?:=(.*))?$/', $v, $matches)) {
$myGET[urldecode($matches[1])] = urldecode($matches[2]);
}
}
I should think not. What exactly are you trying to do?
You could use variable(0)(1) or variable_0_1 as names for example.
Don't believe you can do that. I also don't understand why you'd need to. But this should work:
$_POST['variable'] = array(array('abc','def'),array('ddd','ggg'));
print_r(flatPost('variable'));
function flatPost($var)
{
return enforceString($_POST[$var], $var);
}
function enforceString($data, $preKey = '')
{
if(!is_array($data))
{
return array($preKey . $data);
}
$newData = array();
foreach($data as $key => &$value)
{
$element = enforceString($value, $preKey . '[' . $key . ']');
$newData = array_merge($newData, $element);
}
return $newData;
}
It's a little over the top, but if necessary you could manually parse the request body.
<?php
if(!empty($_POST) && $_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
$_post = array();
$queryString = file_get_contents('php://input'); // read the request body
$queryString = explode('&', $queryString); // since the request body is a query string, split it on '&'
// and you have key-value pairs, delimited by '='
foreach($queryString as $param) {
$params = explode('=', $param);
if(array_key_exists(0, $params)) {
$params[0] = urldecode($params[0]);
}
if(array_key_exists(1, $params)) {
$params[1] = urldecode($params[1]);
}
else {
$params[1] = urldecode('');
}
$_post[$params[0]] = $params[1];
}
$_POST = $_post;
}
?>

Categories