PHP: How to get the default value of the include_path: get_include_path

Various functions in PHP have options to search for files in a fixed set of directories often known as include_path. When you include another PHP file in another PHP program, the interpreter searches for the file in the directories mentioned in the include_path and reports an error if not found. The include_path value can be overwritten by a program. To get the default value of the include_path

<?php
print get_include_path()."n";
?>

The output of the file is generated as follows

$php value.php
.:/usr/share/pear:/usr/share/php

Git: How to recursively add the files in a directory

If you are working with Git and want to add a new file or directory, follow the following steps

Go to the directory where your files are located.

$ cd directory
$ git add *

Now you must commit these changes (You can give necessary comments)

$ git commit -m "Adding new files"

If you want to add a directory and all the files which are located inside it recursively, Go to the directory where the directory you want to add is located.

$ cd directory
$ git add directoryname

Now you must commit these changes (You can give necessary comments)

$ git commit -m "Adding a new directory"

Note that the above step will commit recursively the subdirectories and files present in the directory.

Git: How to add new files and directories?

If you are working with Git and want to add a new file or directory, follow the following steps

Go to the directory where your file is located.

$ cd directory
$ git add filename

Now you must commit these changes (You can give necessary comments)

$ git commit -m "Adding a new file"

If you want to add a directory, Go to the directory where the directory you want to add is located.

$ cd directory
$ git add directoryname

Now you must commit these changes (You can give necessary comments)

$ git commit -m "Adding a new directory"

Note that the above step will also commit the subdirectories and files present in the directory.

PHP: Extract Outgoing URLs from a Web Page

In PHP, you can download a web page using file_get_contents or curl. Once you have downloaded a web page, you can process it.

We know that the tag structure of hyperlink is as follows

<a href="http://www.example.com">Example</a>

Keeping this in mind, we write the following program

<?php

function extractElementsFromWebPage($webPage, $tagName) {
  //Creating a DOMDocument Object.
  $dom = new DOMDocument;

  //Parsing the HTML from the web page
  if ($dom->loadHTML($webPage)) {
    // Extracting the specified elements from the web page
    @$elements = $dom->getElementsByTagName($tagName);
    return $elements;
  }
  return FALSE;
}

function downloadURL($URL) {
  $webPage = file_get_contents ($URL);
  return $webPage;
}

$webPage = downloadURL("http://www.mozilla.org/");
if ($webPage ) {
  $URLs = extractElementsFromWebPage($webPage, 'a');
  if ($URLs) {
    foreach ($URLs as $URL){
      // Extracting the URLs
      echo $URL->getAttribute('href'), "n";
    }
  }
  else {
    echo "Error in parsing the webPagen";
  }
}
else {
  echo "Error in downloading the webPagen";
}
?>

There are certain things that need to be understood:

Firstly we are using file_get_contents to download a web page. Then we use the DOMDocument class in PHP to parse the HTML page. Check the two functions

  1. downloadURL
  2. extractElementsFromWebPage

downloadURL uses file_get_contents to download the web page and extractElementsFromWebPage uses the DOMDocument class. The function loadHTML is used to parse the HTML page and getElementsByTagName to extract the specified elements. In our case, we want to extract the HTML tag element a.

On executing the program

$ php extractURLs.php 
#main
/
/about/
/community/
/projects/
/contribute/
/about/mission.html
http://www.mozilla.com/firefox/
http://www.mozilla.com/mobile/download/
...

PHP: Extract Image URLs from a Web Page

In PHP, you can download a web page using file_get_contents or curl. Once you have downloaded a web page, you can process it. We want to extract the image URLs from a web page.

We know that the tag structure of an image url is as follows

<img src="image.gif" alt="Image Description" />

Keeping this in mind, we write the following program

<?php

function extractElementsFromWebPage($webPage, $tagName) {
  //Creating a DOMDocument Object.
  $dom = new DOMDocument;

  //Parsing the HTML from the web page
  if ($dom->loadHTML($webPage)) {
    // Extracting the specified elements from the web page
    @$elements = $dom->getElementsByTagName($tagName);
    return $elements;
  }
  return FALSE;
}

function downloadURL($URL) {
  $webPage = file_get_contents ($URL);
  return $webPage;
}

$webPage = downloadURL("http://www.mozilla.org/");
if ($webPage ) {
  $imageURLURLs = extractElementsFromWebPage($webPage, 'img');
  if ($imageURLURLs) {
    foreach ($imageURLURLs as $imageURL){
      // Extracting the URLs
      echo $imageURL->getAttribute('src'), "n";
    }
  }
  else {
    echo "Error in parsing the webPagen";
  }
}
else {
  echo "Error in downloading the webPagen";
}
?>

There are certain things that need to be understood:

Firstly we are using file_get_contents to download a web page. Then we use the DOMDocument class in PHP to parse the HTML page. Check the two functions

  1. downloadURL
  2. extractElementsFromWebPage

downloadURL uses file_get_contents to download the web page and extractElementsFromWebPage uses the DOMDocument class. The function loadHTML is used to parse the HTML page and getElementsByTagName to extract the specified elements. In our case, we want to extract the HTML tag element img.

On executing the program

$ php extractImageURLs.php
/images/promos/join_promo_a.png
/images/template/screen/logo_footer.png
https://statse.webtrendslive.com/dcsis0ifv10000gg3ag82u4rf_7b1e/njs.gif?dcsuri=/nojavascript&WT.js=No&WT.tv=8.6.2

PHP: Extract HTML Tags/Element from a Web Page

In PHP, you can download a web page using file_get_contents or curl. Once you have downloaded a web page, you can process it. Take for example, we want to extract the image URLs from a web page.

We know that the tag structure of an image url is as follows

<img src="image.gif" alt="Image Description" />

Keeping this in mind, we write the following program

<?php

function extractElementsFromWebPage($webPage, $tagName) {
  //Creating a DOMDocument Object.
  $dom = new DOMDocument;

  //Parsing the HTML from the web page
  if ($dom->loadHTML($webPage)) {
    // Extracting the specified elements from the web page
    @$elements = $dom->getElementsByTagName($tagName);
    return $elements;
  }
  return FALSE;
}

function downloadURL($URL) {
  $webPage = file_get_contents ($URL);
  return $webPage;
}

$webPage = downloadURL("http://www.mozilla.org/");
if ($webPage ) {
  $imageURLURLs = extractElementsFromWebPage($webPage, 'img');
  if ($imageURLURLs) {
    foreach ($imageURLURLs as $imageURL){
      // Extracting the URLs
      echo $imageURL->getAttribute('src'), "n";
    }
  }
  else {
    echo "Error in parsing the webPagen";
  }
}
else {
  echo "Error in downloading the webPagen";
}
?>

There are certain things that need to be understood:

Firstly we are using file_get_contents to download a web page. Then we use the DOMDocument class in PHP to parse the HTML page. Check the two functions

  1. downloadURL
  2. extractElementsFromWebPage

downloadURL uses file_get_contents to download the web page and extractElementsFromWebPage uses the DOMDocument class. The function loadHTML is used to parse the HTML page and getElementsByTagName to extract the specified elements. In our case, we want to extract the HTML tag element img.

On executing the program

$ php extractElements.php
/images/promos/join_promo_a.png
/images/template/screen/logo_footer.png
https://statse.webtrendslive.com/dcsis0ifv10000gg3ag82u4rf_7b1e/njs.gif?dcsuri=/nojavascript&WT.js=No&WT.tv=8.6.2

PHP: Download Web Page using file_get_contents

You can use curl to download a webpage in PHP. It is also possible to download a web page using file_get_contents().

<?php

function downloadURL($URL) {
  $webpage = file_get_contents ($URL);
  return $webpage;
}

$webpage = downloadURL("http://www.mozilla.org/");
if ($webpage){
  echo $webpage;
}
else {
  echo "Error in downloading the webpagen";
}
?>

$ php download.php
<html>
....
</body>
</html>

In the above example, we try to download the web page of Mozilla. Let’s try to download a non existing web page

<?php

function downloadURL($URL) {
  $webpage = file_get_contents ($URL);
  return $webpage;
}

$webpage = downloadURL("http://www.mozilla.org/1");
if ($webpage){
  echo $webpage;
}
else {
  echo "Error in downloading the webpagen";
}
?>

We find the following error

$ php download.php

Warning: file_get_contents(http://www.mozilla.org/1): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in /home/user/Documents/Dropbox/Personal/Programs/downloadWebpage.php on line 4
Error in downloading the webpage