{PHP} Split text string - php

So I am probably making api system and I am trying to get the client req api.
I checked some tutorials and I found I could use :
$data = $_SERVER['QUERY_STRING'];
It's works but I am getting string like : action=get&id=theapikeygoeshere.
And I need to get only the text after id= , How can I do that?
Thanks!

parse the query string using parse_str:
<?php
$arr = array();
parse_str('action=get&id=theapikeygoeshere', $arr);
print_r($arr);
?>
This gives:
Array
(
[action] => get
[id] => theapikeygoeshere
)

I think the best thing is to use $_GET['id'] but if you want to extract any thing from the QUERY_STRING use
parse_str($_SERVER["QUERY_STRING"], $output);
var_dump($output["id"]);

You can do so by using $_GET['id']. :) $_GET can be used for any URL parameters like those.
E.g.:
$info = $_GET['id']

Related

PHP get user id in url 4

I want to get the id from my url like below
http://localhost/cpanel-ar/stage_one/reports.php?temp=temp_21day
How can GET only the "temp"?
I assume that you mean this:
$getVars = array_keys($_GET);
print_r($getVars);
This will return an array with your get parameters.
for example:
http:example.com?getparameter=getvalue
returns:
Array ( [0] => getparameter )
why you dont try to use substr function?
$data = $_GET['temp'];
echo substr($data,0,4);
its will only catch temp
i dont know if i understood but you can use predefined variables using:
$temp = $_GET['temp'];
but if you are using a framework maybe they already is handle that.
if you want the 'temp' name and you know witch parameters are passed to URL you can use
array_keys($_GET)[i]
Where i i the index of parameter

extract specific data from array in mysql database using php

This is the data that i need to extract like example profile_contact_numbers
so the output will be +639466276715
how can i do it in php code??
any help will do regards
a:2:
{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}
I'm not sure 100% this can go into an array but try the unserialize function
$json_resp = {your values};
$array[] = unserialize($json_resp);
To check if it has gone into an array print_r on $array.
Read this link if the code above doesn't work
http://php.net/manual/en/function.unserialize.php
I have managed to fix it
$serialized = array(unserialize('a:2:{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}'));
var_dump($serialized);
use code :
$var = preg_split('["]','{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}');
echo $var[1].'='.$var[3]; // profile_contact_numbers=+639466276715
echo $var[5].'='.$var[7]; // profile_position=Courier

PHP Build url query from array

I'm trying to build URL query from an Array that looks like that:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
I would like to get query like that:
localhost/get?serial=3804689&serial=3801239&serial=3555689
(you get the idea)
I'm trying to use http_build_query($serials, 'serial', '&'); but it adds the numeric index to the prefix 'serial'.
Any idea how to remove that numeric index?
Well you can't really have the same GET parameter in the string, After all if you try to access that variable server side, what would you use?
$_GET['serial'] - But which serial would it get?
If you really want to get a list of serials, Simply turn the array into a string, save it as an array and there you go. for example :
$serials = "string of serials, delimited by &";
Then you can use the http build query.
Maybe use a foreach:
$get = "localhost/get?serial=" . $serials[0];
unset( $serials[0] );
foreach( $serials AS serial ){
$get .= "&serial=$serial;
}
Just as an FYI, PHP doesn't handle multiple GET variables with the same name natively. You will have to implement something fairly custom. If you are wanting to create a query string with multiple serial numbers, use a delimiter like _ or -.
Ex: soemthing.com/serials.php?serials=09830-20990-91234-12342
To do something like this from an array would be simple
$get_uri = "?serial=" . implode("-", $serials);
You would be able to get the array back from a the string using an explode to
$serials = explode("-", $_GET['serials']);
Yes its quite possible to have such format, you have to build it query string by indices. Like this:
No need to build the query string by hand, use http_build_query() in this case:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$temp = $serials;
unset($serials);
$serials['serial'] = $temp;
$query_string = http_build_query($serials);
echo urldecode($query_string);
// serial[0]=3804689&serial[1]=3801239&serial[2]=3555689&serial[3]=3804687&serial[4]=1404689&serial[5]=6804689&serial[6]=8844689&serial[7]=4104689&serial[8]=2704689&serial[9]=4604689
And then finally, if you need to process it somewhere, just access it thru $_GET['serial'];
$serials = $_GET['serial']; // this will now hold an array of serials
You can also try this
$get = "localhost/get?serial=";
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$list = implode("&serial=",$serials);
echo $get.$list;
Having the same GET parameter will not work this way. You will need to use an array in the get parameter:
localhost/get?serial[]=3804689&serial[]=3801239&serial[]=3555689
IF you just print $_GET through this URL, you will receive
Array ( [serial] => Array ( [0] => 380468 [1] => 3801239 [2]=> 3555689) )
Then you can do this to build your query string
$query_str = 'serial[]='.implode('&serials[]=',$serials);
You can try this version.
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$get = implode('&serials[]=',$serials);
echo 'test';
var_dump($_GET['serials']);
Result

php issue accessing array

I have the following code :
$results = $Q->get_posts($args);
foreach ($results as $r) {
print $r['trackArtist'];
}
This is the output :
["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]
My question is, if trackArtist is an array, why can't I run the implode function like this :
$artistString = implode(" , ", $r['trackArtist']);
Thanks
UPDATE :
Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also.
There must be some processing done in the back.
Any idea how I can extract the information, for example from :
["DUKY","LOQUACE"]
to get :
DUKY, LOQUACE
Thanks for your time
It's probably a JSON string. You can do this to get the desired result:
$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:
var_dump($r['trackArtist']);
To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.

getting variables from STRING url in php

If I have a url that looks like this, what's the best way to read the value
http://www.domain.com/compute?value=2838
I tried parse_url() but it gives me value=2838 not 2838
Edit: please note I'm talking about a string, not an actual url. I have the url stored in a string.
You can use parse_url and then parse_str on the query.
<?php
$url = "http://www.domain.com/compute?value=2838";
$query = parse_url($url, PHP_URL_QUERY);
$vars = array();
parse_str($query, $vars);
print_r($vars);
?>
Prints:
Array
(
[value] => 2838
)
For http://www.domain.com/compute?value=2838 you would use $_GET['value'] to return 2838
$uri = parse_url($uri);
parse_str($uri['query'], $params = array());
Be careful if you use parse_str() without a second parameter. This may overwrite variables in your script!
parse_str($url) will give you $value = 2838.
See http://php.net/manual/en/function.parse-str.php
You should use GET method e.g
echo "value = ".$_GET['value'];

Categories