[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1183
  • Last Modified:

how to escape a quote

In a script that does a search and replace in multiple documents I am doing the following.

$search=""MEDHOD="POST"";
$replace=""METHOD="GET"";

The problem is that the quotes that are in the string that I am searching for conflict with the quotes that surround the entire string in perl.  

How do I make it so this works.

Thanks

0
1stomni
Asked:
1stomni
  • 2
1 Solution
 
olthoffCommented:
Two possible ways

$search='"MEDHOD="POST"';
$replace='"METHOD="GET"';

OR

$search="\"MEDHOD=\"POST\"";
$replace="\"METHOD=\"GET\"";
0
 
jhurstCommented:
$search="\"METHOD=\"POST\"";

However, I think you are still one set short since you will note that there are quotes starting at METHOD and ending after the =
0
 
1stomniAuthor Commented:
This worked :) thanks....

$search='"MEDHOD="POST"';
$replace='"METHOD="GET"';

However I need one more thing.....

When I tried to do this it can't find it when the question mark is in there...  I have tried with and without the \.  If I change the string to only include up to php3 it works, only when I include the question mark it won't find it... please help :)  I included the script below.

$search="http://www.1888wwsports.com/phpshop/cart.php3\?action=add";

$replace="http://www.1888wwsports.com/phpshop/cart.php3";


#!/bin/perl
#
# Kevin Kadow (kadokev@msg.net) <A HREF="http://www.msg.net/kadow/">http://www.msg.net/kadow/</A>
# Can be downloaded from
#<A HREF="http://www.msg.net/utility/small/search.html">http://www.msg.net/utility/small/search.html</A>
# For 1stomni, free for all to use as long as the comments are kept intact!
#
# Search and replace strings in html files
#
require 'find.pl';

#
# Change these variables as needed.
#
$dir="/export/home/worldwide/htdocs/clothingtest";

$search="http://www.1888wwsports.com/phpshop/cart.php3\?action=add";

$replace="http://www.1888wwsports.com/phpshop/cart.php3";

warn <<"EOF";
Starting search/replace script $0
  Search all files below $dir
  Looking for string $search
  Replace the string with $replace

EOF


#
# Do the actual work
#
&find($dir);
warn "Scanned $files making $changes changes.\n";
exit(0);

################ SUBROUTINES FOLLOW #################################

#wanted
#
# This routine is called by 'find', once for each file, divectory, etc.
#
sub wanted {

#Skip everything except files named .html or .shtml
#return unless( -f $_ && m/\.[s]*html$/);
#Skip everything except files named .html or .shtml
return unless( -f $_ && m/\.s?HTML?$/i);

&update("$dir/$_");
}


#update
#
# Do the search-and-replace on a given file
#
sub update {
local($file)=@_;

local(@contents);

# Open the file for reading and writing (the + does that)
unless(open(FILE,"+<$file")) {
      warn "Cannot open $file for updating, Error: $!\n";
      return;
      }

warn "Updating $file\n";
$files++;

# Read the contents into an array, replacing the string as we go
while ($_=<FILE>) {
      $changes += s/$search/$replace/ig;
      push(@contents,$_);
      }

# Go to the beginning of the file
seek(FILE,0,0);

# Update the contents with the data from our array
print FILE (@contents);
close(FILE);
}

###EOF###

0
 
olthoffCommented:
I have the same problem and haven't figured out.  I use index and substr as a replacement.

$x = index($Search, $Replace);
if ($x > -1) {
  $_ = substr($_, 0 $x) . $Replace . substr($_, $x+length($Search)+1);
};

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now