Horje

Tips (Total 9)


# Tips-1) How to extract Domain Name from Full URL in PHP

Simple PHP Fucnction to extract Domain Name from full urls.

PHP Function for extracting Domain Name

Create a function.php file and insert following code
function.php
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 Code For Extracting

Create an index.php file and run following code
index.php
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
?>

Full completed Code for Extracting Domain

Here is full completed code.
index.php
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
?>

Output should be:

Full completed Code for Extracting Domain

# Tips-2) How to extract only name from domain in PHP

Here gives a example to extract name without extension from an url

Full completed code for getting only name from domain url

Just create a file index.php and insert following code.
index.php
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
?>

Output should be:

Full completed code for getting only name from domain url

# Tips-3) How to extract All url from a single page by PHP

Simple way extracts all url from a single page link with PHP

Completed PHP Extracting Code

Use Follow PHP Code
index.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 Demo Screenshot in output area

Output should be:

A Demo Screenshot in output area

# Tips-4) How to get first Five Line from CSV File

A small change to your code should read a maximum of five lines.

How to extract first Five Line from CSV File

Follow the Example.
index.php
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);
?>

Output should be:

How to extract first Five Line from CSV File

# Tips-5) How to get first 100 records from a csv file in PHP

Here is simple way to get first 100 records from csv file.

How to extract first 100 records from a TXT file in PHP

Follow the Example
index.php
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);
?>

Output should be:

How to extract first 100 records from a TXT file in PHP

# Tips-6) How to get meta Tag

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.

How to extract Keyword Description Author from Meta Tag a HTML Page using URL

Just follow Example.

index.php
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>';
?>

Output should be:

How to extract Keyword Description Author from Meta Tag a HTML Page using URL

# Tips-7) How to get title description image and multiple images from URL

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.

Full Code

Just download index.php and run into your website.

You can change the url horje.com and put your once.

index.php
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;

?>

Output should be:

How to extract title description og image keyword other image from URL

Simplely run following codes to your site by chaning http url.

index.html
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/>'; 

 
?>

Output should be:

How to extract title description og image keyword other image from URL

# Tips-8) How to read a txt file from a zip remote URL

Here makes it simple.

How it will work?

  1. First, It will download the zip file to path
  2. Second, It will extract text file that you want to read to index.php
  3. Third, It will auto delete zip file which saved path,

How to read a text file inside zip file of a remote url using PHP

Follow the following codes.

Install Guide.

  1. Create a folder named path in your domain
  2. Change folder permission of folder: path
  3. Create a file named index.php in your domain
  4. Insert following code inside index.php
  5. Replace Line 3 and add your remote url.
  6. Replace Line 14 #wpforms-lite/readme.txt and add Your Zip File's txt destination.
  7. Save it and Run into Browser.

 

index.php
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);
}
?>

Output should be:

How to read a text file inside zip file of a remote url using PHP

# Tips-9) How to extract all url from a html single page in PHP

Normal way of extracting urls from web-page (not recommended)

index.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 />';
    }
}
?>

Output should be:



Share on: