How to extract specific data from JSON encoded string - php

I am writing some code and when I echo this code
echo json_encode($_SERVER);
I get this output
{
"HTTP_CONTENT_TYPE":"application\/x-www-form-urlencoded",
"HTTP_USER_AGENT":"WordPress\/5.7.2; https:\/\/dvsucb.local",
"REQUEST_TIME_FLOAT":1625414926.336319,
"REQUEST_TIME":1625414926
}
I want to extract the URL inside HTTP_USER_AGENT that will be: https://dvscucb.local
I want to extract only the URL not the WordPress version. How to get this ?

You don't need to use json_encode() here.
You can access the HTTP_USER_AGENT property from the $_SERVER array, and then use a regular expression to extract the URL from the string.
Here is a fairly simple example:
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/WordPress\/[\d]+.[\d]+.[\d]+;\s(.*)/", $userAgent, $matches)) {
echo $matches[1];
}

Related

I need to extract exact url using php including the id in the address

I need to extract following url using php : "http://www.website.com/profile#username". All the methods that I have tried return "http://www.website.com/profile"
Just use parse_url()
$url = 'http://www.website.com/profile#username';
$parse = parse_url($url);
print $parse['host']; // prints 'website.com'

How to get the URL of a webpage in PHP?

I need to get the URL of a website, but how do we do this in PHP?
For example there's a URL www.example.com/page.php?var=value, the URL is dynamic, I need to get the var=value portion of the URL. The URL is some other website so cannot use $_SERVER[] variables.
I cannot parse the URL since parse_url() requires an URL to be specified and I don't know the what the value of var will be, I want to fetch the URL using PHP script and then parse it.
Is there any way we can do this in PHP?
parse_url will parse the URL and return its components.
Reference: http://php.net/manual/en/function.parse-url.php
<?php
$url = 'www.example.com/page.php?var=value';
$query_string = parse_url($url, PHP_URL_QUERY );
echo $query_string;
?>
Output:
var=value
With parse_url() and parse_str() you can get the parameters:
$url = "www.example.com/page.php?var=value";
$urlParts= parse_url($url); // Separates the url
parse_str($urlParts['query'], $parameters);// Get the part you actually need it and create a array
var_dump($parameters) // Your parameters
Update:
Replace the var_dump to:
foreach($parameters as $key => $value) {
echo $key." = ".$value."<br />";
}

How to correctly encode URL?

I want to encode like what browser did,
For example. https://www.google.com.hk/search?q=%2520你好
Should be encoded as https://www.google.com.hk/search?q=%2520%E4%BD%A0%E5%A5%BD
I am using the following regex to encode URI without like rawurlencode encode ~!##$&*()=:/,;?+'
$url = 'https://www.google.com.hk/search?q=%2520你好';
echo preg_replace_callback("{[^0-9a-z_.!~*'();,/?:#&=+$#]}i", function ($m) {
return sprintf('%%%02X', ord($m[0]));
}, $url);
But this will return https://www.google.com.hk/search?q=%252520%E4%BD%A0%E5%A5%BD which has an extra 25.
How can I correctly encode the URL that user inputed without modifying original address?
You can simply use urlencode for this purpose. It will encode %2520你好 into %252520%E4%BD%A0%E5%A5%BD . Use the code below.
<?php
$url = 'https://www.google.com.hk/search?q=%2520'.urlencode("你好").'';
echo $url;
?>
I think this will give you your desired url

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>";

In PHP, How to search and match a specific string with JSON data?

I have a bunch of strings and I want to know if they exist in the JSON response.
I used the following code but it is not working. I do not want to loop through the JSON data.
$url = "https://graph.facebook.com/me/feed?access_token=".$session['access_token'];
$page=file_get_contents($url);
$json_a = json_decode($page,true);
$pos = strpos($page, $mystring);
if($pos == true)
{
do something
}
$page does not contain a string with the contents of the JSON feed. How to convert JSON to string so I can check for $mystring ?
EDIT:
This is strange.
When I use the url https://graph.facebook.com/me/feed?access_token=".$session['access_token']
I get empty data, but when I use "https://graph.facebook.com/me/friends?access_token=".$session['access_token'];
Everything is fine and I get the list of all my friends. I am not able to understand where I am going wrong?
This is the link http://developers.facebook.com/docs/reference/api/
I used the exact url format for Profile feed (Wall): https://graph.facebook.com/me/feed?access_token=...
JSON IS a string, until you run it through the decode function and its gets converted to a php structure. You should be able to do
if (strpos($page, "your search string") !== FALSE) {
echo "hey, it's in there";
}
If your $page is coming out blank, then your file_get_contents call is failing somehow.

Categories