I want to give users the possibilily to search and replace words in an article, just for fun 😉. Here is the code i'm using:
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">
<input type=\"text\" name=\"find_what\" value=\"";
if (isset($_POST['find_what'])) {
echo $_POST['find_what'];
} else {
echo "find what?";
}
echo "\" size=\"20\" maxlength=\"25\"/>
<input type=\"text\" name=\"replace_with\" value=\"";
if (isset($_POST['replace_with'])) {
echo $_POST['replace_with'];
} else {
echo "replace with what?";
}
echo "\" size=\"20\" maxlength=\"25\"/>
<input class=\"button\" name=\"submit\" value=\"Replace it!\" type=\"submit\" />
</form>\n";
And to process the form, i have this:
if (isset($_POST['find_what']) && isset($_POST['replace_with'])) {
$_POST['find_what'] = preg_replace("/[_\W]/i", "", $_POST['find_what']);
$_POST['replace_with'] = preg_replace("/[_\W]/i", "", $_POST['replace_with']);
$article = str_replace($_POST['find_what'], $_POST['replace_with'], $article);
}
echo $article;
Everything works like a charm but there are issues i'd like to improve. My questions are:
1) How to search/replace whole words only?
2) How to make the search case insensitive?
3) How to add the Reset button to the form, if possible? This line:
<input type=\"reset\" name=\"reset\" value=\"Reset\" />
doesn't seem to do anything. 🙁 Tried a JavaScript solution i googled but it doesn't work for me either.
Since i'm a newbie, i feel totally lost and would be grateful for any help.