I am trying to create a server side find and replace script. I borrowed some of the code from another script, but the main part of the script was encoded. I am a newbie to PHP but have been reading at php.net and MelonFire.com's free tutorials. here is what I have in my script: <?php // Sets location to search $location = $_POST['location']; // The string to replace $find = $_POST['find']; // The string that replaces $find $replace = $_POST['replace']; // Runs through text to replace $find $output = str_replace($find , $replace , $location); // Display the output of what happened echo $output; ?> PHP: Whenever I run it from the form page, all it outputs is the $location. I know i'm doing something wrong and I don't know is str_replace() is the best function for this. Thanks, Brandon
Looks fine to me, can you try adding the following code at the end, just so we can see if your post form is right. echo "<br>Looking for: $find <br>Replacing with: $replace <br>"; Code (php):
Added something similar: <?php // Sets location to search $location = $_POST['location']; // The string to replace $find = $_POST['find']; // The string that replaces $find $replace = $_POST['replace']; // Runs through text to replace $find $output = str_replace($find , $replace , $location); // Display the output of what happened print "location: " . $location . "<br>"; print "find: " . $find. "<br>"; print "replace: " . $replace. "<br>"; echo $output; ?> PHP: It shows fine, must be a problem with my form page...I'll attach it. I borrowed that part of the code and am trying to learn PHP.
Are you trying to find and replace multiple (or multiline) keywords? If so, you should parse and split keywords before replacing (using str_replace) process. For example: <?php $find = explode("\n", $_POST['find']); $replace = explode("\n", $_POST['replace']); $result = str_replace($find, $replace, $_POST['location']); ?> PHP: But the keyword (line) counts ("find" and "replace" inputs' line count) must be same. PS: str_replace() functions is case sensetive, if you want it case insensetive you can use str_ireplace() function instead of str_replace() function.
For my test I was just trying to replace 'ab' with 'abcd' Nothin special. See any problems with my index.php form that I uploaded?
Do you want to replace a "string" or "file content"? Your index.php file seems to form to replace file contents for specific directory/path, but your code only replaces "location" string coming from that form. If you want to replace file contents of given directory, you should make some directory/file operations. Ie, opening directory, reading all files that matches your query and like these... Here is the simple directory opening and file content replacement code: <?php $dir = opendir($path); while (($file = readdir($dir)) !== false) { if (!is_dir($file)) { $content = file_get_contents($path."/".$file); $output = str_replace($find, $replace, $content); # For PHP 5 use this: file_put_contents($path."/".$file, $output); # For PHP 4 use this: $fp = fopen($path."/".$file, "w"); fwrite($fp, $output); fclose($fp); } } closedir($dir); ?> PHP: So this is only a simple example, there is no error checking, etc and do not forget to backup your files and directory before testing this code. You can explain what you want to do exactly, so I can try to help you to solve your problem and finish your PHP course