Tuesday, 8 April 2014

Creating a find and replace web application in Php

Find and Replace web application in php
Creating a Find text and Replace Web Application In Php, How to find any word or text from the paragraph and replace with new text from exiting by php scripts. Creating a find replace web application in php programming language. The application will take data from user it will search and replace the word inside the statement.


Code For Creating a find and replace web application:- \


<?php



$offset=0;



if(isset($_POST['text'])&&isset($_POST['searchfor'])&&isset($_POST['replacewith'])) {



$text=$_POST['text'];

$search = $_POST['searchfor'];

$replace = $_POST['replacewith'];



$search_length = strlen($search);



if(!empty($text)&&!empty($search)&&!empty($replace)) {



while($strpos = strpos($text, $search, $offset )) {



 $offset = $strpos + $search_length;

 $text = substr_replace($text, $replace, $strpos, $search_length);



echo $text;



}



} else {

  echo 'Please Fill in all fields.';

}



}





?>



<form action="sandr.php" method="POST">

<textarea name="text" rows="6" cols="30"> </textarea><br><br>

Search for:<br>

<input type="text" name="searchfor"><br><br>

Replace With:<br>

<input type="text" name="replacewith"><br><br>

<input type="submit" value="Find and Replace">

</form>

Output (Screenshot1):-

Finding web application in php
Screenshot1
replacing web application in php
Screenshot2
In this example we had created web application inside php programming language. Now i will explain each line of the program if you not understanding anything i had comment option into my blog please comment your queries i will get soon to you.

$offset = 0; // variable is for increment in program 

If in program there are two times cat word is used so program will search first word and position from that word will be set to offset and again it will search second word in program

if(isset($_POST['text'])&&isset($_POST['searchfor'])&&isset($_POST['replacewith'])) {

In this line i had used if condition of php for check fetching variables from forms to the php if you not aware with form please check out html tutorials for forms isset , $_POST are the Global variables of Php and used in html forms and text , searchfor and replacewith are the variables which are in forms name.

$text=$_POST['text'];
$search = $_POST['searchfor'];
$replace = $_POST['replacewith'];

Storing forms variable to defined variables the declaration of variables will be defined after if condition other wise it will give undefined index error.

$search_length = strlen($search);

For getting the length of the searching word strlen function in php is used and storing to $search_length. strlen function had argument the word which we had to find length.

if(!empty($text)&&!empty($search)&&!empty($replace)) {

If condition to check whether user had inputed data to form empty is html function for check

while($strpos = strpos($text, $search, $offset )) {

 $offset = $strpos + $search_length;
 $text = substr_replace($text, $replace, $strpos, $search_length);

echo $text;

}

} else {
  echo 'Please Fill in all fields.';
}

}

With the help of while statement in php we are implementing function of our php for searching and replacing strpos function in php used to find the position where the word is in string which takes three argument the string in which word is the word which for search here offset is 0. 

$offset = $strpos + $search_length

Here we are incremental the position if we had searched first word it will search other similar word into the string. If you not getting me watch my article on string position function in php

$text = substr_replace($text, $replace, $strpos, $search_length);


substr_replace function in php will used to replace the string which had four arguments $text is the string , $replace is the word which we want to replace $strpos is the position in string at which position the word is and the $search length is the length the replace word count function in php.


} else {
  echo 'Please Fill in all fields.';
}

}


validation if user will leave any field it will saw this message to the user.

<form action="sandr.php" method="POST">
<textarea name="text" rows="6" cols="30"> </textarea><br><br>
Search for:<br>
<input type="text" name="searchfor"><br><br>
Replace With:<br>
<input type="text" name="replacewith"><br><br>
<input type="submit" value="Find and Replace">
</form>

Is the html form to take the data from the user . In this way you can make web application in php. It was the little example of dynamic web application of php programming language.




No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates