Please wait while we load your page...
 

Announcements

SearchReplace

More Tools. . .

We provide this tool for FREE! However, we would greatly appreciate your support so we can continue to develop more tools (online tools and downloadable tools). You can show your support by sharing / liking this tool on Facebook. You can also tweet this page to your Twitter followers and following us on Twitter.


SearchReplace is a web-based tool that allows you to search and replace any text in files on your server. Or, you can just conduct a search without replacing. Got 1000s of HTML files on your server that contain text like an affiliate link code that you need to replace? Then SearchReplace is the tool for you! Save yourself hours or even days of manually editing 100s or 1000s of files (HTML, PHP, etc.). Do one directory, one directory and all nested subdirectories, one entire site, or even an entire server!

sr.php


<?php
##########################################################################
#  SearchReplace Version 2.00201510091
#
#  Created:		Oct 09, 2015
#  Updated:		Oct 09, 2015
#  Copyright:	2015 Password Sentry. All Rights Reserved
#
#  URL:			https://www.password-sentry.com/searchreplace/
##########################################################################
#########################################################################
#  Copyright Notice
##########################################################################
#  Any redistribution of this script without the expressed written consent
#  of Password Sentry is strictly prohibited. Copying any of the code
#  contained within this script and claiming it as your own is also
#  prohibited. You may not remove any of these header notices. By using
#  this code you agree to indemnify Password Sentry from any liability
#  that might arise from its use.
##########################################################################
#  Installation
##########################################################################
#  1. Name file as searchreplace.php
#  2. Upload to web server (Either upload to password protected directory,
#     or delete from web server after done)
#  3. Call script from web browser.
##########################################################################
#  Do NOT change or alter the code below!
##########################################################################
error_reporting(0);
if (function_exists('date_default_timezone_set')) {
	date_default_timezone_set('UTC');
}
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SearchReplace</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<style type="text/css">
body {
	color:						#FFFFFF;
}
a.link {
	color:						#FFFFFF;
	font-family:				Verdana, Helvetica, sans-serif;
	font-size:					12px;
	font-style:					normal;
	font-weight:				bold;
	text-decoration:			none;
}
a.link:hover {
	color:						#FFFFFF;
	font-family:				Verdana, Helvetica, sans-serif;
	font-size:					12px;
	font-style:					normal;
	font-weight:				bold;
	text-decoration:			underline;
}
a.link2 {
	color:						#0EB493;
	font-family:				Verdana, Helvetica, sans-serif;
	font-size:					12px;
	font-style:					normal;
	font-weight:				bold;
	text-decoration:			none;
}
a.link2:hover {
	color:						#0EB493;
	font-family:				Verdana, Helvetica, sans-serif;
	font-size:					12px;
	font-style:					normal;
	font-weight:				bold;
	text-decoration:			underline;
}
.main_box {
	background:					rgba(14, 180, 147, 1);
	border:						1px solid #FFFF00;
	border-radius:				30px;
	box-shadow:					#EAEAEA 15px 15px 15px;
	height:						900px;
	margin-left:				auto;
	margin-right:				auto;
	width:						78%;
	-webkit-border-radius:		30px;
	-moz-border-radius:			30px;
	-webkit-box-shadow:			#EAEAEA 15px 15px 15px;
	-moz-box-shadow:			#EAEAEA 15px 15px 15px;
}
.special {
	color:						#337147;
	text-decoration:			none;
	font-weight:				bold;
}
.special:hover {
	color:						#337147;
	text-decoration:			underline;
	font-weight:				bold;
}
.title_bar {
	font-size:					20px;
	color:						#FFF;
	font-weight:				bold;
	margin:						10px;
	padding:					10px;
	text-align:					center;
	background:					#0EB493;
}
.errorbox {
	background-color:			#FFEBE8;
	border:						1px solid #DD3C10;
	color:						#333333;
	padding:					10px;
	font-size:					13px;
	font-weight:				bold;
	text-align:					center;
	width:						100%;
}
.successbox {
	background-color:			#E1FAEA;
	border:						1px solid #10DD43;
	color:						#333333;
	padding:					10px;
	font-size:					13px;
	font-weight:				bold;
	text-align:					center;
}
.button {
	border-radius:				10px;
	-moz-border-radius:			10px;
	-webkit-border-radius:		10px;
	background:					#0EB493;
	font-size:					14px;
	border:						solid 2px #FFFFFF;
	border-radius:				10px;
	color:						#FFFFFF;
	display:					inline-block;
	font:						bold 14px Helvetica, Arial, sans-serif;
	margin-right:				10px;
	padding:					7px 7px;
	position:					relative;
	text-decoration:			none;
	transition:					border-color .218s;
}
.button:hover {
	background:					#0B9075;
	color:						#FFFFFF;
	border-color:				#FFFFFF;
	box-shadow:					0 1px 2px rgba(0, 0, 0, 0.15);
}
.button:active {
	color:						#FFFFFF;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<?php
$version	= '2.00201510091';
$result		= get_file('http://www.password-sentry.com/scripts/version/version.php?product=sr');
function get_file($source) {
	if (function_exists('curl_init')) {
		$ch			= @curl_init($source);
		@curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		@curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		@curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		$contents	= @curl_exec($ch);
	}
	else { $contents = @file_get_contents($source); }
	return $contents;
}
if ($result and ($version != $result)) { $alert = "Your App is Out-of-Date :: Please <a class='link2' href='https://www.password-sentry.com/searchreplace/' target='new'>Upgrade</a>"; }
else { unset($alert); }
if ($_POST['action'] == "save") { showfile($_POST['filename'], $_POST['filecontents'], 1); }
if ($_GET['filename']) { showfile($_GET['filename'], "", 0); }
if (! $_POST and ! $_GET) {
?>
<div class="main_box">
	<table style="width:90%;" align="center">
		<tr>
			<td class="title_bar">SearchReplace Version <?php echo $version; ?></td>
		</tr>
		<?php if ($alert) { ?>
		<tr>
			<td class="errorbox"><?php echo $alert; ?></td>
		</tr>
		<?php } ?>
		<tr>
			<td>
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" target="results" name="submission">
			<table style="width:90%;">
				<tr>
					<td>
					<a class="special" href='javascript:alert("Enter directory path to look for files. The default path is the directory that this script is installed. Your path as been detected as [<?php echo getcwd(); ?>]. If the path is blank, then enter the path here - if in doubt, then ask your web host. Otherwise, no need to edit.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Search Directory Path</b></td>
					<td><input type="text" name="path" value="<?php echo getcwd(); ?>/" size="100" style="width:100%;" /></td>
				</tr>
				<tr>
					<td>
					<a class="special" href='javascript:alert("Enter text to search for. Text string can be over multiple lines, delineated by carriage returns. Not recommended that you enter too much text, as otherwise the script may time out or stall due to excessive load on server.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Find</b></td>
					<td><textarea name="find" rows="3" cols="40" style="width:100%;"></textarea></td>
				</tr>
				<tr>
					<td>
					<a class="special" href='javascript:alert("Enter text to replace [Find] text with. Text string can be over multiple lines, delineated by carriage returns. Not recommended that you enter too much text, as otherwise the script may time out or stall due to excessive load on server. Optional, and not required if you are only searching.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Replace</b></td>
					<td><textarea name="replace" rows="3" cols="40" style="width:100%;"></textarea></td>
				</tr>
				<tr>
					<td>
					<a class="special" href='javascript:alert("Select if you want to conduct a case-sensitive search or replace. Default is enabled.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Case Sensitive</b></td>
					<td><input type="checkbox" name="case" checked="checked" value="1" style="background-color: #FFFFFF" /></td>
				</tr>
				<tr>
					<td>
					<a class="special" href='javascript:alert("Select if you only want to show files where [Find] string found. Default is enabled.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Show Only Found</b></td>
					<td><input type="checkbox" name="showonly" checked="checked" value="1" style="background-color: #FFFFFF" /></td>
				</tr>
				<tr>
					<td>
					<a class="special" href='javascript:alert("Select if you want to search all nested subdirectories within the search directory. Default is disabled.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Include Subdirectories</b></td>
					<td><input type="checkbox" name="sub" value="1" style="background-color: #FFFFFF" /></td>
				</tr>
				<tr>
					<td>
					<a class="special" href='javascript:alert("Only applies if you are replacing. Select if you want to create backups of any files that are modified in the replacement process. Backup files are created in the same directory as the modified file, and have a .bak file extension. Highly recommended in case you run into problems - for example, if file corrupted during replacement process, you can restore original file from backup (by deleting new file, and renaming backup name of original file). Default is enabled.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Make Backups</b></td>
					<td><input type="checkbox" name="backup" checked="checked" value="1" style="background-color: #FFFFFF" /></td>
				</tr>
				<tr>
					<td>
					<a class="special" href='javascript:alert("Select which files you wish to search on, based on file extension. Default is [HTML]. If replacing, it is not recommended that you select [All Files] unless you are sure of what you are doing - for example, given directory (and subdirectories if you have enabled [Include Subdirectories]) only contain files that you want to conduct replacements - and no Binary files like image, sound, video files, etc. You can also enter [Custom Extensions] if you want to include file types not included in the drop-down list - enter file extensions (with leading period) on one line, separated by commas.");'>
					<i class="fa fa-question-circle fa-lg"></i></a>
					<b>Files to Search</b></td>
					<td>
					<select name="ext[]" multiple="multiple" size="4" style="width:100%;">
						<option value="all">All Files (*.*)</option>
						<option value="htm">HTM (*.htm)</option>
						<option value="html" selected="selected">HTML (*.html)</option>
						<option value="php">PHP (*.php)</option>
					</select><br/>
					<b>Custom Extensions</b> : <input type="text" name="customext" style="width:100%;" /><br/>
					(separate by commas - for example: txt,dat,log)<br/></td>
				</tr>
				<tr>
					<th colspan="2">
					<input class="button" type="submit" name="action" value="search" />
					<input class="button" type="submit" name="action" value="replace" onclick="return confirm('Perform [Search and Replace]? This action cannot be undone.');" />
					</th>
				</tr>
				<tr>
					<td colspan="2">
					<iframe style="background-color:#FFFFFF;width:100%;height:300px;" name="results"></iframe>
					</td>
				</tr>
				<tr>
					<td colspan="2" style="text-align:center;"><b>© Copyright <?php echo date('Y'); ?> <a class="link" href="https://www.password-sentry.com/searchreplace/">Password Sentry</a>.
					All Rights Reserved.</b></td>
				</tr>
			</table>
			</form>
			</td>
		</tr>
	</table>
</div>
</body>
</html>
<?php
exit;
}
elseif ($_POST) {
	if (get_magic_quotes_gpc()) {
		function stripslashes_deep($value) {
			$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
			return $value;
		}
		$_POST	= array_map('stripslashes_deep', $_POST);
	}
	$action		= $_POST['action'];
	if (! $action) { doerror("No [Action] selected!"); }
	if (($action != "search") && ($action != "replace")) { doerror("Invalid [Action] passed!"); }
	$path		= $_POST["path"];
	if (! $path) { doerror("No Required [Search Directory Path] entered!"); }
	if (! preg_match("/\/$/", $path)) { $path .= "/"; }
	if (preg_match('/\.\./', $path)) { doerror("Illegal [Search Directory Path] entered!");}
	if (! file_exists($path)) { doerror("[Search Directory Path] does not exist!"); }
	if (! is_dir($path)) { doerror("[$path] is not a valid directory!");}
	$find		= $_POST['find'];
	if (! $find) { doerror("No Required [Find Text] entered!"); }
	$find		= preg_quote($find);
	$find		= preg_replace("/\//", "\/", $find);
	$replace	= $_POST['replace'];
	$replace	= preg_replace('/\$/', '\$', $replace);
	$sub		= $_POST['sub'];
	$case		= $_POST['case'];
	$showonly	= $_POST['showonly'];
	$ext		= $_POST['ext'];
	foreach ($ext as $value) { $extension[$value] = 1; }
	$customext	= trim($_POST['customext']);
	$list		= preg_split("/,/", $customext);
	foreach ($list as $value) {
		$value = strtolower($value);
		$match = preg_match("/^[a-zA-Z0-9]+$/", $value);
		if (! $match) { $invalids .= "$value | "; }
		else { $extension[$value] = 1; }
	}
	$invalids	= rtrim($invalids, " | ");
	if ($invalids) { doerror("Invalid [Custom Extensions] - $invalids"); }
	$myfiles	= spider_directory($path, $sub, $extension);
	sort($myfiles);
	$index = 0; $items = 0; $found = 0; $tfound = 0; $replacements = 0; $treplacements = 0; $fdone = 0;
	foreach ($myfiles as $value) {
		if ($index % 2) { $color = "#EAEAEA"; }
		else { $color = "#FFFFFF"; }
		list($found, $replacements, $res)	= process_file($value);
		$tfound								= ($tfound + $found);
		$treplacements						= ($treplacements + $replacements);
		if (! $found and $showonly) { }
		else {
			if (! $found) { $fcolor = "red"; }
			else { $fcolor = "green"; }
			$results .= "					<tr id=\"tr1$index\">\n";
			$results .= "						<td bgcolor=\"$color\" align=\"left\"><a class=\"link2\" href=\"#\" onclick=\"$('#tr1$index').css('display', 'none');$('#tr2$index').css('display', 'none');return false;\" ><i class=\"fa fa-times-circle\"></i></a><input type=\"text\" name=\"\" value=\"$value\" size=\"120\" /><br/><b><font color='#0EB493'>$res</font></b></td>\n";
			$results .= "						<td bgcolor=\"$color\" align=\"left\"><a class=\"link2\" href=\"#\" onclick=\"window.open('sr.php?filename=$value', name, 'scrollbars=1,resizable=no,width=1000,height=500,status=0,menubar=0'); return false;\" title=\"Show File\">Show File</a></td>\n";
			$results .= "					</tr>\n";
			$results .= "					<tr  id=\"tr2$index\" colspan='2'><td> </td></tr>\n";
			if ($found) { $fdone++; }
			$index++;
		}
		$items++;
	}
	$f			= stripslashes($find);
	$r			= stripslashes($replace);
	if ($action == "search") { $msg = " :: Searching for $f"; }
	else { $msg = " :: Replacing $f with $r"; }
	echo "<br/><br/><table style='color:#000000;'>";
	echo "<tr><td><b>Number Files Processed:</b></td>";
	if (! $items) { $color = "red"; }
	else { $color = "green"; }
	echo "<td style='background-color:$color;color:#FFFFFF;padding:2px;width:100px;'>$items</span></td></tr>";
	echo "<tr><td><b>Number Files Flagged:</b></td>";
	if (! $fdone) { $color = "red"; }
	else { $color = "green"; }
	echo "<td style='background-color:$color;color:#FFFFFF;padding:2px;width:100px;'>$fdone</td></tr>";
	echo "<tr><td><b>Total Number Found:</b></td>";
	if (! $tfound) { $color = "red"; }
	else { $color = "green"; }
	echo "<td style='background-color:$color;color:#FFFFFF;padding:2px;width:100px;'>$tfound</td></tr>";
	echo "<tr><td><b>Total Number Replacements:</b></td>";
	if (! $replacements) { $color = "red"; }
	else { $color = "green"; }
	echo "<td style='background-color:$color;color:#FFFFFF;padding:2px;width:100px;'>$treplacements</td></tr>";
	echo "</table><br/><br/>";
	echo "<table width='100%'>";
	echo $results;
	echo "</table>";
	exit;
}
function doerror($error, $id) {
	echo "<div style='background-color:#FFEBE8;border:1px solid #DD3C10;color:#333333;padding:10px;font-size:13px;font-weight:bold;text-align:center;'>$error</div>";
	exit;
}
function showfile($filename, $file, $action) {
	if (! $filename) { doerror("No file passed"); }
	if (! file_exists($filename)) { doerror("File not found"); }
	if (preg_match('/\.\./', $filename)) { doerror("Illegal file path passed"); }
	if ($action) {
		$file	= preg_replace('/<\/textarea>/', '</textarea>', $file);
		$handle	= @fopen($filename, 'w+');
		if (! $handle) {
			$perm		= substr(decoct(fileperms($filename)), 2);
			chmod($filename, 0666);
			$handle		= @fopen($filename, 'w+');
			if (! $handle) {
				chmod($filename, $perm);
				$msg	= "<div class=\"errorbox\">Cannot open file for writing! Try CHMOD the file as 666 or 777 via FTP, and try again. Afterwards, CHMOD back to $perm</div>";
			}
			else {
				@fwrite($handle, $file);
				@fclose($handle);
				chmod($filename, $perm);
				$msg	= "<div class=\"successbox\">File saved!</div>";
			}
		}
		else {
			@fwrite($handle, $file);
			@fclose($handle);
			$msg	= "<div class=\"successbox\">File saved!</div>";
		}
	}
	$file	= file_get_contents($filename);
	$file	= preg_replace('/<\/textarea>/', '</textarea>', $file);
	
print <<<HTML
<table align="center">
	<tr>
		<td>$msg</td>
	</tr>
	<tr>
		<td bgcolor="#FFFFFF">
		<form action="" method="post">
			<textarea name="filecontents" cols="90" rows="20">$file</textarea><br/>
			<input type="hidden" name="filename" value="$filename" />
			<input class="button" type="submit" name="action" value="save" onclick="return confirm('Save file? This action cannot be undone.');" />
			<input class="button" type="submit" value="close" onclick="self.close ();" />
		</form>
		</td>
	</tr>
</table>
</body>
</html>
HTML;
exit;
}
function spider_directory($directory, $sub = 1, $extension, $exclude = '') {
	$arrayItems		= array();
	$skipByExclude	= false;
	$handle			= opendir($directory);
	if ($handle) {
		while (false !== ($file = readdir($handle))) {
			preg_match("/(^(([\.]){1,2})$|(\.(svn|git|md))|(Thumbs\.db|\.DS_STORE))$/iu", $file, $skip);
			if ($exclude) {
				preg_match($exclude, $file, $skipByExclude);
			}
			if (! $skip && ! $skipByExclude) {
				if (is_dir($directory. DIRECTORY_SEPARATOR . $file)) {
					if ($sub) {
						$arrayItems = array_merge($arrayItems, spider_directory($directory. DIRECTORY_SEPARATOR . $file, $sub, $extension, $exclude));
					}
				}
				else {
					$parts	= preg_split("/\./", $file);
					while (list($key, $val) = each($parts)) { $e = $val; }
					if (($extension[$e]) || ($extension['all'])) {
						$file			= $directory . DIRECTORY_SEPARATOR . $file;
						$arrayItems[]	= $file;
					}
				}
			}
		}
		closedir($handle);
	}
	return $arrayItems;
}
function process_file($filename) {
	global $find, $replace, $case, $showonly, $backup, $action;
	$chunksize	= (1 * (1024 * 1024));
	$buffer 	= '';
	$perm		= substr(decoct(fileperms($filename)),3);
	$handle		= fopen($filename, 'rb');
	if ($handle === false) { return array(0, 0, "Cannot read file - file has $perm permissions: try chmoding as 666"); }
	while (! feof($handle)) { $buffer = fread($handle, $chunksize); }
	if (! fclose($handle)) { return array(0, 0, "Cannot close file"); }
	if (! $case) { $match = preg_match_all("/$find/i", $buffer, $matches); }
	else { $match = preg_match_all("/$find/", $buffer, $matches); }
	$nummatches	= count($matches[0]);
	if ($action == "search") { return array($nummatches, 0, "$nummatches Found"); }
	elseif ($action == "replace") {
		if ($backup) { if (! copy($filename, "$filename.bak")) { return array(1, 0, "Replacement skipped since cannot create backup file - either chmod the directory 777 or disable backups"); } }
		if (! $case) { $buffer = preg_replace("/$find/i", $replace, $buffer); }
		else { $buffer = preg_replace("/$find/", $replace, $buffer); }
		$perm		= substr(decoct(fileperms($filename)),3);
		$handle		= fopen($filename, 'w');
		if ($handle === false) { return array(1, 0, "Cannot read file - file has $perm permissions: try chmoding as 666"); }
		if (fwrite($handle, $buffer) === FALSE) { return array(1, 0, "Cannot update file"); }
		if (! fclose($handle)) { return array(1, 0, "Cannot close file"); }
		return array($nummatches, $nummatches, "$nummatches Found and Replaced $nummatches Times");
	}
} 
?>


Live Chat Not Available