Looking for extracting of data from URL like Facebook. In this tutorial we see how to extract data from URL or Link. You have seen extracting data from URL using DOM parser but here we are talking about extract data in PHP using jQuery from URL. Very simple script in PHP to separate title, image and meta description of a webpage.In this tutorial we have created two webpage one for sending data using ajax and other to get results. Simple code to extract data from links in PHP using jQuery. Let us see the snippet below to understand better.
HTML
<input type="text" name="links" id="links"> <div id="result" ></div>
JAVASCRIPT
$(“#links”).keyup(function() – using this script on keyup call a fetch function which retrieves the value of URL from textbox by document.getElementById(‘links’).value. Response get on success is display in div which have ID “result”.
<script type="text/javascript" src="jquery.js"></script> <script> $(document).ready(function(){ $("#links").keyup(function(){ fetch(); }); }); function fetch() { var linkdata= document.getElementById('links').value; if(linkdata.length>0) { $("#result").html('<img src="ajax-loader.gif"/>'); $.ajax({ type:'POST', url:'result.php', cache:false, data:{link:linkdata}, success:function(response){ $('#result').slideDown(); document.getElementById('result').innerHTML= response; } }); }} </script>
CSS
img { width:auto; height:auto; } #result { width:450px; height:auto; margin-top:5px; }
result.php
This webpage contains a file_get_contents() function of PHP to get remote content which loads URL data. After getting data we are splitting it separately using preg_match() function to get title, meta description and image.
<?php $linkss=$_POST['link']; if($linkss){ function get_content($url){ $vikash = @file_get_contents($url); $res = preg_match("/<title>(.*)<\/title>/", $vikash, $title_matches); $title = preg_replace('/\s+/', ' ', $title_matches[1]); $title = trim($title); preg_match('/<img[^>]+>/',$vikash, $result); //store image in $result $image=$result[0]; $tags = get_meta_tags($url); echo $image; echo '<a href="'.$url.'">'.'<h1>'.$title.'</h1>'.'</a>'; echo $tags['description']; // a php manual } get_content($linkss); } ?>