SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP File Function

Searching for title inside a file

We can collect the text written between two landmarks inside a file. These landmarks can be starting and ending of html tags. So what ever written within a tag can be copied or collected for further processing. Before we go for an example, read the tutorial on how to get part of a string by using one staring and ending strings.

Let us try to understand this by an example. We will try to develop a script which will search and collect the text written within the title tags of a page. Read here if you want to know more about title tags in an html page. Here is an example of title tag.

<title>This is the title text of a page</title>

As you can see within the page we can use starting and ending title tags or any pair of tags as two landmarks and collect the characters or string within it.

Now let us learn how to open a file and read the content. Here is the code part to do that.
$url="../dir-name/index.php";
$contents="";
$fd = fopen ($url, "r"); // opening the file in read mode
while($buffer = fread ($fd,1024)){
$contents .=$buffer;
}
Now as we have the content of the file stored in a variable $content , we will use our function my_strip ( read details about my_strip function here) to collect the title part only from the variable and print to the screen.
$t=my_strip("<title>","</title>",$contents);
echo $t;
With this we can give any URL and see the title of the file. This way like title tag we can read any other tag like meta keywords, meta description, body tag etc of a page. You can see many applications can be developed using this but let us try to develop few more things from this.

First is reading all the files of a directory and displaying all the titles of the files inside that directory
Second develop a hyperlink using these titles to query google for these titles. ( think why ? )

The above two codes we will discuss in next section. Before that here is the full code as we discussed in the above tutorial.
<?
///////////////function my_strip/////////// function my_strip($start,$end,$total){ $total = stristr($total,$start); $f2 = stristr($total,$end); return substr($total,strlen($start),-strlen($f2)); } /////////////////////End of function my_strip /// ///////////// Reading of file content//// $url="../dir-name/index.php";// Right your path of the file $contents=""; $fd = fopen ($url, "r"); // opening the file in read mode while($buffer = fread ($fd,1024)){ $contents .=$buffer; } /////// End of reading file content //////// //////// We will start with collecting title part /////// $t=my_strip("<title>","</title>",$contents); echo $t; ?>
Once we know the title text , we can go for str_ireplace command to replace the old title with new title and then write the content to file again.

Searching for files and reading content inside a directory

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer