Simple PHP Fucnction to extract Domain Name from full urls.
Example:
PHP
<?php
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
?>
Example:
PHP
<?php
include("function.php");
$domain_name = 'https://horje.com/learn/511/how-to-set-html-autofocus-attribute-in-input';
$domain_name = get_domain($domain_name);
echo $domain_name;
// Result: horje.com
?>
Example:
PHP
<?php
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
?>
<?php
$domain_name = 'https://horje.com/learn/511/how-to-set-html-autofocus-attribute-in-input';
$domain_name = get_domain($domain_name);
echo $domain_name;
// Result: horje.com
?>
Here gives a example to extract name without extension from an url
Example:
PHP
<?php
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
?>
<?php
$domain_name = 'https://horje.com/learn/511/how-to-set-html-autofocus-attribute-in-input';
$domain_name = get_domain($domain_name);
$domain_name = pathinfo($domain_name, PATHINFO_FILENAME);
echo $domain_name;
// Result: horje
?>
Simple way extracts all url from a single page link with PHP
Example:
PHP
<?php
$urlContent = file_get_contents('http://php.net');
$dom = new DOMDocument();
@$dom->loadHTML($urlContent);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for($i = 0; $i < $hrefs->length; $i++){
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
$url = filter_var($url, FILTER_SANITIZE_URL);
// validate url
if(!filter_var($url, FILTER_VALIDATE_URL) === false){
echo '<a href="'.$url.'">'.$url.'</a><br />';
}
}
?>
A small change to your code should read a maximum of five lines.
Example:
PHP
<?php
$maxLines = 5;
$file_handle = fopen("file.csv", "r"); // Here put CSV File Location
for ($i = 0; $i < $maxLines && !feof($file_handle); $i++)
{
$line_of_text = fgetcsv($file_handle, 1024);
print '<div class="ticker_price">'. $line_of_text[0] . "</div>";
}
fclose($file_handle);
?>
Here is simple way to get first 100 records from csv file.
Example:
PHP
<?php
$start = 1;
$end = 100;
$handle = fopen("file.csv", "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(($start <= $i) && ($i <= $end)) {
echo $data[0];
echo '</br>';
}
elseif($i > $end) {
break;
}
$i++;
}
fclose($handle);
?>
Returns an array with all the parsed meta tags.
The value of the name property becomes the key, the value of the content property becomes the value of the returned array, so you can easily use standard array functions to traverse it or access single values. Special characters in the value of the name property are substituted with '_', the rest is converted to lower case. If two meta tags have the same name, only the last one is returned.
Returns false
on failure.
Just follow Example.
Example:
PHP
<?php
// Assuming the above tags are at www.example.com
$tags = get_meta_tags('https://www.metkovicnet.com');
// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author']; // name
echo '</br>';
echo $tags['keywords']; // php documentation
echo '</br>';
echo $tags['description']; // a php manual
echo '</br>';
echo $tags['geo_position']; // 49.33;-86.59
echo '</br>';
echo $tags['og:image']; // 49.33;-86.59
echo '</br>';
?>
The following PHP code shows how to get page titles and other meta details by using PHP CURL. In this code, I initialized the CURL object and set the URL to be accessed with the reference of this object. The CURL script will return the HTML content of the remote page.
After getting the HTML content, we need to parse the HTML by referring to the title, meta and img tag names to get the page title, description and the image URLs, respectively. These data are encoded into a JSON array and returned as the AJAX response.
Just download index.php and run into your website.
You can change the url horje.com and put your once.
Example:
PHP
<?php
$url = "https://horje.com/learn/757/clipbucket";
// Extract HTML using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
// Load HTML to DOM Object
$dom = new DOMDocument();
@$dom->loadHTML($data);
// Parse DOM to get Title
$nodes = $dom->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
// Parse DOM to get Meta Description
$metas = $dom->getElementsByTagName('meta');
$body = "";
for ($i = 0; $i < $metas->length; $i ++) {
$meta = $metas->item($i);
if ($meta->getAttribute('name') == 'description') {
$body = $meta->getAttribute('content');
}
}
// Parse DOM to get Images
$image_urls = array();
$images = $dom->getElementsByTagName('img');
for ($i = 0; $i < $images->length; $i ++) {
$image = $images->item($i);
$src = $image->getAttribute('src');
$m_i .= ''.$src.'</br>';
}
echo $title;
echo '</br>';
echo $body;
echo '</br>';
echo $src;
echo '</br>';
echo $m_i;
?>
Simplely run following codes to your site by chaning http url.
Example:
PHP
<?php
// Web page URL
$url = 'https://horje.com';
// Extract HTML using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
// Load HTML to DOM object
$dom = new DOMDocument();
@$dom->loadHTML($data);
// Parse DOM to get Title data
$nodes = $dom->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
// Parse DOM to get meta data
$metas = $dom->getElementsByTagName('meta');
$description = $keywords = '';
for($i=0; $i<$metas->length; $i++){
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description'){
$description = $meta->getAttribute('content');
}
if($meta->getAttribute('name') == 'keywords'){
$keywords = $meta->getAttribute('content');
}
if($meta->getAttribute('property')=='og:image'){
$meta_og_image = $meta->getAttribute('content');
}
}
$src = preg_match('/<img.+src=[\'"]http(?P<src>.+?)[\'"].*>/i', $data, $result);
$img = 'http'.$result[1].'';
echo "Title: $title". '<br/>';
echo "Description: $description". '<br/>';
echo "Keyword: $keywords". '<br/>';
echo "OG Image: $meta_og_image". '<br/>';
echo "Imgage: $img". '<br/>';
?>
Here makes it simple.
How it will work?
Follow the following codes.
Install Guide.
Example:
PHP
<?php
$url = 'https://downloads.wordpress.org/plugin/wpforms-lite.1.9.2.1.zip';
$destination_dir = 'path/';
if (!is_dir($destination_dir)) {
mkdir($destination_dir, 0777, true);
}
$local_zip_file = basename(parse_url($url, PHP_URL_PATH)); // Will return only 'some_zip.zip'
if (!copy($url, $destination_dir . $local_zip_file)) {
die('Failed to copy Zip from ' . $url . ' to ' . ($destination_dir . $local_zip_file));
}
// Change the txt file location which is inside of the zip file Example: #wpforms-lite/readme.txt
$result = file_get_contents('zip://path/'.$local_zip_file.'#wpforms-lite/readme.txt');
# Display txt file's texts
echo '<xmp>'.$result.'</xmp>';
$result = file_get_contents('zip://path/'.$local_zip_file.'#readme.txt');
echo $result;
$zip = new ZipArchive();
if ($zip->open($destination_dir . $local_zip_file)) {
$zip->close();
// Clear zip from local storage:
unlink($destination_dir . $local_zip_file);
}
?>
Normal way of extracting urls from web-page (not recommended)
Example:
PHP
<?php
$urlContent = file_get_contents('http://php.net');
$dom = new DOMDocument();
@$dom->loadHTML($urlContent);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for($i = 0; $i < $hrefs->length; $i++){
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
$url = filter_var($url, FILTER_SANITIZE_URL);
// validate url
if(!filter_var($url, FILTER_VALIDATE_URL) === false){
echo '<a href="'.$url.'">'.$url.'</a><br />';
}
}
?>