sub search_replace
{
 # Declare all our variables as local to this subroutine:
 my ($name,$value,%entry,@templatefile,$upper,$cookie,$link,@newentry);
 
 my ($thisentry) = shift;						# Get the hash reference provided to work with.
 my ($template) = shift;						# Also get the template file to use.
 my ($noparse) = shift;							# Should we bypass ubb and emoticon parsers ?
 
 if (shift){ $emailhide = 0; }
 
 @templatefile = getfile($template,0); 			# Get the template file specified above.
 $cookie = retrieve_cookie("gwad") || ''; 		# Retrieve any existing admin cookie.
 %entry = %{$thisentry};						# Create a true hash from the reference provided.

 foreach my $key (keys %entry){
  $entry{$key} = split_long($entry{$key});		# Feed data through long word filter.
  $entry{$key} = smut_filter($entry{$key});		# Feed data through smut filter.
 }
 
 if (!$noparse){
  foreach my $key (keys %entry){
   $entry{$key} = ubb_code($entry{$key});		# Feed data through the ubb_code filter.
   $entry{$key} = emoticonify($entry{$key});	# Feed data through emoticon filter.
   $entry{$key} = html_ify($entry{$key},$key);	# Feed data through 'htmlify' filter.
  }
 }


	# Now we want to replace the special entry content tags (see manual) with actual data.
	# We loop over each line of the template and do the following:

	# 1. If the config is set to hide email address and there is no valid admin cookie:
	# 	a. Prepare a link to the admin login, so we can authenticate to view emails.
	# 	b. Replace the email field content tag ("%%EMAIL%%") with the above link.

	# 2. Loop over each field name in the entry hash (%entry) as do as follows:
	# 	a. Copy the individual field name to $upper and convert to upper case.
	# 	b. Search for any occurances of entry content tags, and replace them with the data.
	# 	  (The content tag is comprised of the upper case field name, surrounded by "%%").

	# 3. Now we've replaced all the content tags we do the following:
	# 	a. Copy the new data line by line into an array (@newentry).
	# 	b. Return a reference to to this new array.

 foreach my $line (@templatefile){
  
  if ($emailhide && ($cookie ne $adminpass)){
   $link = qq(<a href="$scripturl?action=admin&return=view">(hidden)</a>);
   $line =~ s!\%\%EMAIL\%\%!$link!g;
  }
  
  foreach my $key (keys %entry){
   $upper = uc($key);
   $line =~ s!\%\%$upper\%\%!$entry{$key}!g;
  }
  
  push (@newentry, $line);
 }
return \@newentry;
}
1;
