I have a string with a URL
$string = "http://www.yahoo.com/foo.php?bar=201";
all i want is the query string without everything else.
obviously cant user query_string, help appreciated.
$parts = parse_url($string);
echo $parts['query'];
http://php.net/manual/en/function.parse-url.php
Related
I have string variable like ABC/DF/G I want extract DF from it means text between two /.
I have tried like below
$string= "ABC/DF/G";
$code = substr($string ,strpos($string,'/'));
echo $code;
but I am getting result like below
/DF/G
Let me know if someone can help me for do it.
Thanks!
You can use explode function to split a string into an array.
$string= "ABC/DF/G";
$code = explode('/', $string);
echo $code[1];
I have some very long URL variables. Here is one example.
http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039
Ultimately it would be nice if I could find a way to use preg_replace to simply change one variable even if in the middle of the string for instance in the string above change print=no to 'print=yes for example.
I will however settle for a preg_replace pattern match that allows me to delete ?image=XYZ_1555025022.jpg. as this is a variable the name could be anything. It will always have "?image" " at the start and end with "&"
I think one of the problems I have run into is that preg_match seems to have issues on strings with "=" contained in them .
I am completely lost here in this and all those characters make may head spin. Maybe someone can give some guidance please?
Here's a demo of how you can do some of things you want using explode, parse_str and http_build_query:
$url = 'http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039';
// split on first ?
list($path, $query_string) = explode('?', $url, 2);
// parse the query string
parse_str($query_string, $params);
// delete image param
unset($params['image']);
// change the print param
$params['print'] = 'yes';
// rebuild the query
$query_string = http_build_query($params);
// reassemble the URL
$url = $path . '?' . $query_string;
echo $url;
Output:
http://localhost/index.php?mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=yes&mode=color&printscalewidth100=&printscaleheight100=&rand=56039
Demo on 3v4l.org
You can use str_replace() or preg_replace() to get your job done, but parse_url() with parse_str() will give you more controls to modify any parameters easily by array index. Finally use http_build_query() to make your final url after modification.
<?php
$url = 'http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039';
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo "BEFORE".PHP_EOL;
print_r($query);
$query['print'] = 'yes';
echo "AFTER".PHP_EOL;
print_r($query);
?>
DEMO: https://3v4l.org/npGij
hi all i am trying to parse a big string and want to obtain only the value of id(id=dfsdfdsfdsfsaddfdfdfd) . My current code output extra data beyon the id value. could any one help me fix this problem.Thanks
preg_match_all("#live3.me.somesite.com([^<]+)\"#", $html, $foo);
print_r($foo[0]);
sample input string:
$html=".............."url":"rtmp:\/\/live3.me.somesite.com\/live\/?id=dfsdfdsfdsfsaddfdfdfd","name":"8.low.stream".........";
Is this an issue where you are set on using preg_match?
Or would something like 'parse_url' be acceptable?
http://php.net/manual/en/function.parse-url.php
Once parsed, getting specific query string parameters out should be easy
Just for the fun of answering an 8 month old question, here is my go at it :-)
We have got this to work with:
$html='".............."url":"rtmp:\/\/live3.me.somesite.com\/live\/?id=dfsdfdsfdsfsaddfdfdfd","name":"8.low.stream"........."';
That string sure looks a bit JSONish to me, so why not ...
// 1. Get rid of leading and trailing quotes and dots
$clean = trim($html, '"');
$clean = trim($clean, '.');
// 2. Add squiggly brackets to form a JSON string
$json = '{' . $clean . '}';
// 3. Decode the JSON into an array
$array = json_decode($json, true);
// 4. Parse URL value from the array and grab the query part
$query = parse_url($array['url'], PHP_URL_QUERY);
// 5. Get parameter values from the query
parse_str($query, $params);
// 6. Ta-da!
echo "Readable : ", $params['id'], PHP_EOL;
And of course ...
// The one-liner!
parse_str(parse_url(json_decode('{'.trim(trim($html,'"'),'.').'}', true)['url'], PHP_URL_QUERY), $params2);
echo "One-liner: ", $params2['id'], PHP_EOL;
Output:
Readable : dfsdfdsfdsfsaddfdfdfd
One-liner: dfsdfdsfdsfsaddfdfdfd
I would like to use one string inside another string, like this:
$url = "http://www.sample.com/index.php?nr=$string[0]";
how should I insert this string at the end?
$url = "http://www.sample.com/index.php?nr={$string[0]}";
or
$url = "http://www.sample.com/index.php?nr=" . $string[0];
just like your example
$url = "http://www.sample.com/index.php?nr=$string[0]";
As a solution to your problem please try executing following code snippet.
You need to concatenate strings in php using dot operator(.)
$url = "http://www.sample.com/index.php?nr=".$string[0];
I want to extract id from an url using php preg_match..
For eg: $string = 'http://www.mysite.com/profile.php?id=1111'; I need output as '1111'
using preg_match.
I tried this :
if(preg_match('/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=[0-9]/', $string, $match) > 0){
$id = $match[1];
}
But am getting output as 'profile.php'
Can someone help me please?
Why not use parse_url with parse_str
$url_component = parse_url($string);
parse_str($url_component['query'], $output);
echo $output['id'];
if there is only one parameter in the url you can use explode function to do that easily, like
$string = 'http://www.mysite.com/profile.php?id=1111';
$ex=explode('=',$string);
$id=$ex[1];
You may also use parse_url function of php.
Hope this help,
$pattern = '/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=([0-9]+)/';
This should work fine!
But do this with parse_url