Creating Content Excerpt Using PHP


Content Excerpt Using PHP Script
Hi Guys, in this post will discuss about how to create content excerpt using php. Previously, we'll try to understand what that content excerpt. If you're familiar with the world of blogging either using wordpress, blogger or any other blog service providers, you will probably notice that the article on the homepage of your blog does not show up as a whole, but rather in the form of excerpts article. Now we will try to make a simple article excerpt using PHP.

We will use MySQL to store the data content and PHP as a scripting language that will handle the process and sends a response to the client. The first we have to create a database first. I'm not going to explain how to create a database in MySQL, but you can get a database dump file export results that have been made. Download my_db.sql.
On the homepage will be shown the first images contained on the content (if any), shown only snippets of text or excerpt but provided links to full post with anchor text "read more". The display on the homepage more or less like this:

content excerpt on the homepage

To get the image url that is the content we can utilize phphtmlparser class are Copyright (c) 2003 Jose Solorzano. Download phphtmlparser. Before proceeding you may need to read my post previously about Getting an Image URL From Using IMG tag php. Here is a function that is used to get the image url in the content that contains html tags:
 include "htmlparser.inc";
 function get_first_image($text){
   $parser = new HtmlParser($text);
   while($parser->parse()){
     if($parser->iNodeName == 'img'){
        return $parser->iNodeAttributes['src'];
     }
   }   
 }
Function used to generate quotations from the content as a whole are as follows:
function excerpt_content($text){
    $split_str=strtok(strip_tags($text)," ");
    $result="";
    for ($i=1;$i<=35;$i++){
      $result .=$split_str;
      $result .=" ";
      $split_str=strtok(" ");
    }
    return $result;
 }
Download PHP Content Excerpt Full Source. After downloading the source code excerpt content using php, you can run it on a local server. The first one you have to do is extract the downloaded file and then copy the folder "content_excerpt" to the root directory on your server. My Server is on the root directory "C: \ xampp \ htdocs".

Extract Content Excerpt Source 
Next you need to create database "my_db", you can make it through phpMyAdmin. Consider the following picture:

create database my_db
Then select the database "my_db" phpMyAdmin on the left sidebar, and then import the file "my_db.sql" by clicking on the phpMyAdmin Import menu as shown in the image below:

Import my_db.sql

Browse file "my_db.sql" which has been included in the folder "content_excerpt" in the root directory of your server after that click "GO" button.

Browse File my_db.sql

Then you need to make adjustments to the file "connect.php" in the second line:
mysql_connect("localhost","root","");
You must adapt to your MySQL server configuration to connect to the database. Note the following syntax:
 mysql_connect("your_server_name_or_ip","your_username","your_password");.

If the adjustment is done, open your browser and then run through the following url "http://localhost/content_excerpt/index.php".