Once you understand how to use forms input data into PHP scripts, you can start using some of the many built in functions to make your own tools. On this page, we will use two string functions to make the search and replace tool above.
To start, you need a form with three textareas; one for the input string, one for the search string and one for the replacement string:
<form method="post" action="/mike/tutorials/php/str_replace.php">
<textarea cols="55" rows="1" growable name="in"></textarea>
<center>
<input type="submit">
</center>
<br><br>
Search for
<textarea cols="55" rows="1" growable name="for"></textarea>
<br><br>
Replace with
<textarea cols="55" rows="1" growable name="with"></textarea>
</form>
The first thing to note is that the form uses the post method. If you use get, the amount of text that you can input is limited. Before the HTML is generated, use an if statement to see if anything has been submitted into to the form:
if ( isset($_POST[in']) )
{ }
If something has been submitted, grab the variables. The inputs may contain HTML. PHP automatically escapes quotes entered into a form with backslashs because it is made work with databases and quotes can't be entered into a database so after you grab the variables, .use stripslashes() to remove the backslashes.
<?
$in = $_POST['in'];
$for = $_POST['for'];
$with = $_POST['with'];
$in = stripslashes($in);
$for = stripslashes($for);
$with = stripslashes($with);
?>
If the outputed string contains </textarea> or </form>, they will they will cause the textarea containing the output to close before it should so you need to replace them with </textarea> and </form>. str_replace is case sensitive so you need to replace <TEXTAREA> and </FORM>. To replace strings with mixed case, you would need to use preg_replace().
<?
$out = str_replace("$for", "$with", $in);
$out = str_replace("</textarea>", "<textarea>", $out);
$out = str_replace("</TEXTAREA>", "<textarea>", $out);
$out = str_replace("</form>", "</form>", $out);
$out = str_replace("</FORM>", "</form>", $out);
?>
Now that you have the three output strings, go back to your form and use in if statements to see if there was anything entered into the textareas and if so, echo the output string into them.
<textarea cols="55" rows="1" growable name="in"><?
if ( isset($_POST['in']) )
{ echo "$out"; }
?></textarea>
?>
<textarea cols=-" rows="1" growable name="for"><?
if ( isset($_POST['for']) )
{ echo "$for"; }
?></textarea>
<textarea cols="55" rows="1" growable name="with"><?
if ( isset($_POST['with']) )
{ echo "$with"; }
?></textarea>
<?
$self = "$_SERVER[PHP_SELF]";
if ( isset($_POST['in']) )
{
$in = $_POST['in'];
$for = $_POST['for'];
$with = $_POST['with'];
$in = stripslashes($in);
$for = stripslashes($for);
$with = stripslashes($with);
$out = str_replace("$for", "$with", $in);
$out = str_replace("</textarea>", "<textarea>", $out);
$out = str_replace("</TEXTAREA>", "<textarea>", $out);
$out = str_replace("</form>", "</form>", $out);
$out = str_replace("</FORM>", "</form>", $out);
# $out = str_replace("mike>", " xxx >", $out);
}
?>
<form method="post" action="<?=$self; ?>">
<textarea cols="55" rows="1" growable name="in"><?
if ( isset($_POST['in']) )
{ echo "$out"; }
?></textarea>
<center>
<input type="submit">
</center>
<br><br>
Search for
<textarea cols="55" rows="1" growable name="for"><?
if ( isset($_POST['for']) )
{ echo "$for"; }
?>
</textarea>
<br><br>
Replace with
<textarea cols="55" rows="1" growable name="with"><?
if ( isset($_POST['with']) )
{ echo "$with"; }
?>
</textarea>
</form>
|
|
|