Hi I writed a test code as below.
<?php
$url = 'http://localhost/events/result/cn_index.php?login';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
Output
Array ( [scheme] => http [host] => localhost [path] => /events/result/cn_index.php [query] => login ) /events/result/cn_index.php
Now I inserted the line below
echo array[query]; // I want to echo 'login', but failed.
How to get the value of 'login'?
$parsed = parse_url($url);
echo $parsed['query'];
Try with:
$output = parse_url($url, PHP_URL_PATH);
echo $output['query'];
Related
Hello I'm trying to get this information from an HTML page but it doesn't have element,class, nothing it just have <pre>all content</pre> how can I extract the value from "validTo"? Information below
It is not a JSON FILE, its just the content of the web page.
[serialNumber] => 984924526890779987
[validFrom] => 180515204024Z
[validTo] => 180807195300Z
[validFrom_time_t] => 1526416824
PHP Code:
<?php
$url = "https://www.google.es";
$orignal_parse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
echo $certinfo
Something like this will work if the page is formatted the way you described.
// get the webpage - replace your.url with the actual webpage address
$html = file('your.url',FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES) ;
// get your value
foreach($html as $line) {
$result = strpos($line,'[validTo]') ;
if (false !== $result) {
$temp = explode('=>', $line) ;
$validTo = trim($temp[1]) ;
break ;
}
}
Consider your file has this content
file.html
[serialNumber] => 984924526890779987
[validFrom] => 180515204024Z
[validTo] => 180807195300Z
[validFrom_time_t] => 1526416824
Now this will extract the info and put it into an array named $arr. Access each by using that array.
<?php
$data = file_get_contents('http://domain/file.html');
$arr = explode(PHP_EOL, $data);
echo $arr[0];
?>
Hi you can extract more info from a string by using Regex.
<?php
$string = "
<pre>
[serialNumber] => 984924526890779987
[validFrom] => 180515204024Z
[validTo] => 180807195300Z
[validFrom_time_t] => 1526416824
</pre>
";
preg_match('/\[validTo\].[\=\>].*[0-9a-zA-Z]/', $string, $values, PREG_OFFSET_CAPTURE);
print_r($values);
The Result was:
Array
(
[0] => Array
(
[0] => [validTo] => 180807195300Z
[1] => 97
)
)
I have url like https://in.pinterest.com/sridharposnic/restinpeace/.
I want url directories without domain in php array.
Example:-
$array[0] = 'sridharposnic';
$array[1] = 'restinpeace';
How we can extract these ?
You can use parse_url() and explode():
$url = 'https://in.pinterest.com/sridharposnic/restinpeace/';
$parsed = parse_url( $url );
$chunks = explode( '/', trim($parsed['path'],'/') );
print_r( $chunks );
Will print:
Array
(
[0] => sridharposnic
[1] => restinpeace
)
Currently, I can strip the domain of the URL string by:
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://www.example.com/foo/bar?hat=bowler&accessory=cane';
if (preg_match($pattern, $url, $matches) === 1) {
echo $matches[0];
}
This will echo:
example.com
The problem is that I want to include the subdomain if it exists.
So for example:
$url = 'http://sub.domain.example.com/foo/bar?hat=bowler&accessory=cane';
Should echo:
sub.domain.example.com
How can I achieve this insanity?
You can use the parse_url() function for this:
$str = 'http://sub.domain.example.com/foo/bar?hat=bowler&accessory=cane';
$parts = parse_url($str);
print_r($parts);
Output:
Array
(
[scheme] => http
[host] => sub.domain.example.com
[path] => /foo/bar
[query] => hat=bowler&accessory=cane
)
Thus:
echo $parts['host'];
Gives you:
sub.domain.example.com
I have following uri intranet/student/main/schedule
I have to take just the name of the directory which is student, but when I try
basename($_SERVER['SCRIPT_NAME']);
it returns index.php, when I try
basename($_SERVER['REQUEST_URI']);
it retutns schedule. How can I take just student or at least student/main/schedule?
Try this....
$url = 'http://www.examplewebsite.com/intranet/student/main/schedule';
$parsed = parse_url($url);
$exploded = explode('/', $parsed['path']);
$directory = $exploded[2];
echo $directory;
Hope this helps.
You can get directory like this-
dirname('c:/Temp/x'); // returns 'c:/Temp'
OR
You can try this-
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
It will gives you
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
I know you can easily get a parameter from the page you're currently on, but can you easily do the same from any URL string?
I need to grab the "id" parameter out of a string like https://market.android.com/details?id=com.zeptolab.ctr.paid?t=W251bGwsMSwxLDIxMiwiY29tLnplcHRvbGFiLmN0ci5wYWlkIl0.
How can I do this with PHP? Is there a built-in function, or do I have to use regex?
You could use combination of parse_url and parse_str:
$url = 'https://market.android.com/details?id=com.zeptolab.ctr.paid?t=W251bGwsMSwxLDIxMiwiY29tLnplcHRvbGFiLmN0ci5wYWlkIl0';
$arr = parse_url($url);
parse_str($arr['query']);
echo $id; //com.zeptolab.ctr.paid?t=W251bGwsMSwxLDIxMiwiY29tLnplcHRvbGFiLmN0ci5wYWlkIl0
Yes you can.
parse_url()
From the PHP docs:
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
The above example will output:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path
There's parse_url():
function extractGETParams($url)
{
$query_str = parse_url($url, PHP_URL_QUERY);
$parts = explode('&', $query_str);
$return = array();
foreach ( $parts as $part )
{
$param = explode('=', $part);
$return[$param[0]] = $param[1];
}
return $return;
}
$url = 'http://username:password#hostname/path?arg=value&arg2=value2#anchor';
var_dump( extractGETParams($url) );
On Codepad.org: http://codepad.org/mHXnOYlc
You can use parse_str(), and then access the variable $id.