SQL PHP HTML ASP JavaScript articles and free scripts to download
 

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.

Discuss this tutorial at forum


Further readings
Reading from local and remote files by using fopen()
Writing to a file by fwrite() function
unlink:Deleting file by using unlink function
Deleting all files present inside a directory
Downloading files using header control
File upload to server using PHP
File upload using PHP 5 and register_global off
Uploading More than one file to server
Displaying all files and directory of a folder
Getting the Present file name running the PHP script
Getting the last updated time of the file in PHP
Displaying the last modification time of the file
Searching for text within a pair of tags of a html page
Listing All title tags inside a directory














 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.