Strip Google search URL to original clean url - php

I am looking for a function that takes a dirty google search URL and returns it clean, as the original URL, means the URL that will show up in your browser after you clicked on the search result and the redirection.
For example, convert this link:
https://www.google.co.il/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiiz-Xp4srYAhXOxqQKHTZeAPQQFggnMAA&url=https%3A%2F%2Fwww.usatoday.com%2F&usg=AOvVaw04_mIwjwWapfFyzAJqqpNW
To this:
https://www.usatoday.com/

You can do this by 'exploding' the string.
<?php
$url = 'https://www.google.co.il/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiiz-Xp4srYAhXOxqQKHTZeAPQQFggnMAA&url=https%3A%2F%2Fwww.usatoday.com%2F&usg=AOvVaw04_mIwjwWapfFyzAJqqpNW';
$url = urldecode($url);
echo $url . '<br>'; //normal url
$url = explode('&',$url);
$url = str_replace('url=','',$url);
echo $url[9]; //the url is the 9th variable in the string/array
?>

Related

How To Auto Detect Url Format ? And Auto Create The Right Secured Echo or Printf?

Trying in building a Searchengine.
Need to echo user submitted links on Searchengine Result Pages. Problem is, different submitted urls will be in different php format.
Q1. How to auto detect the format ?
Q2. My searchengine will be opening the iFrame (of user submitted links) to other users (keyword searchers). How will my php script automatically know which part of the url to run htmlentities() and which parts to run the urlencode() ?
I can't be manually check the url format on a million link each day that my users submit to me everyday.
I mean, if I was opening an iFrame to my own link then no problem as I know my own link's format:
Example:
$url = 'http://localhost/test/pagination.php';
$search = $domain;
$tbl = 'linking_histories';
$col= 'domain';
$i = 1;
$limit = 2;
printf(
"<iframe src='%s?mysql_tbl=%s&mysql_column=%s&keyword_search=%s&result_limit_per_page=%d&page_number=%d'></iframe><br>",
htmlentities($url),
urlencode($tbl),
urlencode($col),
urlencode($search),
$limit, // %d place-holder will force integer
$i,
urlencode($limit),
urlencode($i)
);
I mean, a user might submit a normal static link like so:
A.
'http://localhost/test/pagination.php';
Or, a dynamic one, like these:
B.
'http://localhost/test/pagination.php?keyword=cars'; //%s (printf).
C.
'http://localhost/test/pagination.php?page=4'; //%d (printf).
D.
'http://localhost/test/pagination.php?keyword=cars&page=4';
//%s (printf) & %d (printf).
For example A, this php code is ok to echo the url in the iFrame:
$url = 'http://localhost/test/pagination.php';
printf(
"<iframe src='%s'></iframe><br>",
htmlentities($url),
);
For the example B submitted link, this particular php code is fine to echo the url in the iFrame:
$url = 'http://localhost/test/pagination.php';
$search = $domain;
printf(
"<iframe src='%s?keyword_search=%s'></iframe><br>",
htmlentities($url),
urlencode($search),
);
For example C, this particular php code is correct to echo the url in the iFrame:
$url = 'http://localhost/test/pagination.php';
$i = 1;
printf(
"<iframe src='%s?page_number=%d'></iframe><br>",
htmlentities($url),
$i,
urlencode($i)
);
For D, this particular php code is correct:
$url = 'http://localhost/test/pagination.php';
$search = $domain;
$i = 1;
printf(
"<iframe src='%s?mysql_tbl=%s&mysql_column=%s&keyword_search=%s&page_number=%d'></iframe><br>",
htmlentities($url),
urlencode($search),
$i,
urlencode($i)
);
As you can see from the above, notall the 4 links on the 4 iframes are in same url format.
One uses just htmlentities and no urlencode,
another uses the htmentities plus one urlencode ONLY
while another uses the htmlentities and TWO urlencode
and so on.
Some links have INT while others don't.
Now since each user submitted link will be different to each other, then I can't use one set of printf to echo all url formats.
So how to detect the url format on auto to generate the right printf with the right data type on the printf (eg. '%s', '%d") for that particular url the user submits ?
Is there any function in php that can detect the url type to tell me which functions (htmlentities, urlencode(), %s, %d, etc.) to use on which part of the url ? You know the var_dump() tells you the data type. Something like that I am looking for.
Care to show an code example how to achieve my purpose ?
Remember, I need to secure the link outputs so nobody can inject any link in the iFrames ?
**EDIT:
Do I use htmentities() or urlencode() here ?
Or both ?
Imagine url is either this:
$url = 'http://localhost/test/pagination.php?tbl=links&col=domain&search=elvanja.com&page=1&limit=5';
Or, this:
$url = http://www.elvanja.com/contactus.php;
Example 1:
printf("<iframe src='%s'></iframe><br>",
htmlentities($url));
Example 2:
printf("<iframe src='%s'></iframe><br>",
urlencode($url));
Example 3:
printf("<iframe src='%s?tbl=%s&col=%s&search=%s&limit=%d&page=%d'></iframe><br>",
htmlentities($url),
urlencode($url));
I going for EXAMPLE 3, what you say ?**

Get youtube video ID from a dynamically generated youtu.be link

I'm working with a WordPress plugin that outputs a text link, but the user inputs the link from an 3rd party site. the links are youtu.be format, I need the video's ID. I have tried this method but I'm not getting it so far
original code:
if($video_link != "") {
echo '<p>';
echo 'Please visit : Multimedia link for more photos and information' ;
echo '</p>';
}
Output link :
http://youtu.be/abcdefghijkl
tried this, can't get it to work:
if($video_link != "")
{
$url = $_GET['url'];
$video_id = substr( parse_url($url, PHP_URL_PATH), 1 );
echo '<iframe width="560" height="315" src="https://www.youtube.com/embed'.$video_id.'" frameborder="0" allowfullscreen></iframe>' ;
}
Output Iframe does not work :
EDIT: Ignore my previous answer, your parse works fine. You just forgot the / after 'embed' in your iframe src attribute.
Or if you want, you can just remove the substr part in your parse (which is what strips the slash)
$video_id = parse_url($url, PHP_URL_PATH);

Fetching specific URL with preg_match

This is my code :
$patt = "#href=\"(.*?)\"#";
preg_match($patt,$data,$match);
echo $match[1];`
i.e. theres a URL in the HTML code of the page $data
<a href="http://aba.ai/iEU9x">
I want to grab this link above. Thanks

Get .mp4 source and poster image from Vine Id (PHP)

How to get video.mp4 from vine url?
Example:
from https://vine.co/v/hnVVW2uQ1Z9
I need http://.../*.mp4 and http://.../*.jpg
Script what I need use this page vinebed.com
(In PHP)
Thanks much.
It's very simple. if you check the source of a vine video from vine.co you'll see the meta tags. and you should see twitter:player:stream. By using php you can extract that information specifically and use it like a variable.
<?php
function vine( $id )
{
$vine = file_get_contents("http://vine.co/v/{$id}");
preg_match('/property="twitter:player:stream" content="(.*?)"/', $vine, $matches);
$url = $_SERVER['REQUEST_URI'];
return ($matches[1]) ? $matches[1] : false;
}
?>
And to set an $id you will need to create a function that will either A) Automatically read a vine video id by url and you can display it like this <?php echo vine('bv5ZeQjY35'); ?> or B) Just set a vine video id and display as is.
Hope this helps as it's worked for me just fine.

file_get_contents don't work with some images

I am trying to download an image with file_get_contents PHP's function.
It receive an urlencode(url) via GET and return the content.
This is the code:
<?php
$url=($_GET["url"]);
$url2 = ("http://www.liberoquotidiano.it/resizer.jsp?w=500&h=-1&maximize=true&img=upload/cut1372677360319.jpg&filetype=image.jpg");
echo "<br>Url 1 is via GET <br> Url2 is a variable instantiated in the script and its value is manually inserted.";
echo "<br>file_get_contents Url2 work, but with url1 not,althought the url content is the same. ";
echo "<br>1.url= ".$url;
echo "<br>2.url= ".$url2;
$r=strcmp($url2,$url);
if($r==0){
echo "correct";
}else{
echo "<br><br>string compare with url and url2 return ".$r;
}
echo "<br><br>launch: file_get_contents(url) => ";
$image_data = file_get_contents($url);
echo $image_data;
?>
url and url2 are the same but php strcmp code return 1, and not 0...I don't understand why.
If I launch
file_get_contents($url);
it don't work and I haven't returned any value.
If I launch
file_get_contents($url2);
it correctly work.
The curiousness is that url and url2 contains the same value, but the results differ.
This is the link at the script:
http://www.clouderize.it/michele/get_cont.php?url=http%3A%2F%2Fwww.liberoquotidiano.it%2Fresizer.jsp%3Fw%3D500%26amp%3Bh%3D-1%26amp%3Bmaximize%3Dtrue%26amp%3Bimg%3Dupload%2Fcut1366795446185.jpg%26amp%3Bfiletype%3Dimage.jpg
What may be the problem?
Thank you a lot.
$url = $_GET['url];
Are you using any html sort of things in the form from which you are fetching url. Is it there that user is entering url with html tags too or you are including some html tags with the url. Because, if strcmp is not giving 0 as output, it means that both url strings are not equal. There must be something which is causing the issue. And it can be html tags. Just check it once.
Oks the problem is that in url there are some & I have substituted these with &.
$src=str_replace("&", "&", $src);

Categories