how to split a URL for get the id php - php

i have this URL:
www.blublub.de/niedersachsen.html?veranstaltung=Neumarkt+Osnabrück~W011Cb5ETzN2EJoG9N~10.04.2014
And i like to get the id-number **"W011Cb5ETzN2EJoG9N"** from the URL.
How can i implement this in php ?
please help me.

$parts=explode('~', $_GET['veranstaltung']);
$id=$parts[1];

try this
<?php
$veranstaltung=$_GET['veranstaltung'];
$str=explode('~',$veranstaltung);
$idnumber=$str[1];
echo $idnumber;
?>
get and explode it

try to get and explode with (~) your url query string
$url = $_GET['veranstaltung'];
$myid = explode('~', $url);
echo $myid[1]; // output - W011Cb5ETzN2EJoG9N

Use regular exp for this
if (preg_match_all("/~(.*?)~/",$_GET['veranstaltung'],$result){
print_R($result)
}

Related

PHP trying to use str_replace()

This is my current code:
$parcels = $api->parcels->get();
$url = (array_values($parcels)[0]['label']['label_printer']);
$goToUrl = $api->getUrl($url);
str_replace('/api/v2//api/v2/', '/api/v2/', $goToUrl);
print_r($goToUrl);
echo "<br />";
echo $url;
Why do I use str_replace()? because I am intending to redirect to $goToUrl and this is not working because the current API is giving me the link wrong.
This is my output:
https://api_key:api_secret#panel.sendcloud.nl/api/v2//api/v2/labels/label_printer/1369315
As you can see, api/v2 comes in this link twice. I want to remove /api/v2/ and then run the output. But my str_replace(); is not performing. My output stays the same.
Can this even be achieved this way? Thanks for any help in advance.
Try changing the str_replace line to:
$goToUrl = str_replace('/api/v2//api/v2/', '/api/v2/', $goToUrl);

Output a part of URL in php

So, I have dynamically generated pages that follows the following format:
http://example.com/name/first_name/last_name/John%2C+Smith
What is the php code to output the last part of the url?
so, it becomes like "John, Smith".
Thank you so much.
EDIT:
I realized that the URL is ended with another / and the answers given below does not pick it up. What change should I make?
http://example.com/name/first_name/last_name/John%2C+Smith/
EDIT 2:
So, the link is dynamically generated as the following:
href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
Split the url, get the last fragment then URL decode it:
<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>
You could do this with a regex.
echo preg_replace_callback('~.*/(.*)~',
function($matches) {
return urldecode($matches[1]);
},
'http://example.com/name/first_name/last_name/John%2C+Smith');
Regex demo: https://regex101.com/r/bE3bO5/1
Output:
John, Smith
Update:
echo preg_replace_callback('~.*/(.+)~',
function($matches) {
return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');
You can use parse_url with second parameter PHP_URL_PATH
$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));
Edited:
As per requirement for dynamic url you can use
$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

Short String after php explode

HI I get a string in our data base after completing a quiz which contains the results of each question in a single string. like:
&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3
each question can be skipped for a while to answer later, so the answers are unsorted.
now I want the results like
q1=2,q2=5,q4=9,q6=8,q12=1,q14=7,q19=10,q20=3
Can anyone help me.?
Try this
$a='&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
$b=explode('&',$a);
natsort($b);
$c=implode(',',$b);
print($c);
try like this
$a='&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
$a = ltrim($a,'&');
$b=explode('&',$a);
natsort($b);
echo $c=implode(',',$b);
try this one:
$url = '&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3';
parse_str($url, $urlDecoded);
$urlDecoded = array_flip($urlDecoded);
natsort($urlDecoded);
$urlDecoded = array_flip($urlDecoded);
var_dump($urlDecoded);
Other examples were fine but they all had a leading comma ,.
Here is an improved version:
$a=explode('&','&q1=2&q2=5&q14=7&q6=8&q4=9&q19=10&q12=1&q20=3');
natsort($a);
echo substr(implode(',',$a),1);

php preg_match get everything after match in string

Looking for how to get the complete string in a URI, after the away?to=
My code:
if (isset($_SERVER[REQUEST_URI])) {
$goto = $_SERVER[REQUEST_URI];
}
if (preg_match("/to=(.+)/", $goto, $goto_url)) {
$link = "<a href='{$goto_url[1]}' target='_blank'>{$goto_url[1]}</a>";
The original link is:
https://domain.com/away?to=http://www.zdf.de/ZDFmediathek#/beitrag/video/2162504/Verschw%C3%B6rung-gegen-die-Freiheit-%281%29
.. but my code is cutting the string after the away?to= to only
http://www.zdf.de/ZDFmediathek
You know the fix for this preg_match function to allow really every character following the away?to= ??
UPDATE:
Found out, that $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'] is already cutting the original URL. Do you know why and how to prevent that?
try use (.*) to get all after to=
$str = 'away?to=dfkhgkjdshfgkhldsflkgh';
preg_match("/to=(.*)/", $str, $goto_url);
echo $goto_url[1]; //dfkhgkjdshfgkhldsflkgh
Instead of extracting the URL with regex from the request URI you can just get it from the $_GET array:
$link = "<a href='{$_GET['to']}' target='_blank'>{$_GET['to']}</a>";

fetching the url in php

my url is like below:
http://www.xyz.org/abc/list.php?id=1
now "$_SERVER['PHP_SELF']" gives me only "abc/list.php" this portion
and "$_SERVER['REQUEST_URI']" gives me "abc/list.php?id=1" this
now if i want to fetch only the portion "?id=1" how to do that.
coz im having problems in the paging query for this.
thanxx in advance...
It's $_SERVER['QUERY_STRING'].
Use $_GET['id'] to access the id parameter.
Take a look at http://php.net/parse_str, http://php.net/parse-url and http://php.net/http_build_query
Depending on what you're doing http://bradym.net/php/modify-query-string-parameters may also be helpful.
$_GET['id'] or $_REQUEST['id']
if your not sure which GET values u'll receive u also could
$fullRequest = exlode('?', $_SERVER['REQUEST_URI']);
$params = array();
foreach(explode('&', $fullRequest) as $part){
foreach(explode('=', $part) as $keyVal){
$params[$keyVal[0]] = $keyVal[1];
}
}

Categories