?
Solved

regex -- ? -- to replace semicolons in quotes but leave those outside quotes.

Posted on 2006-04-30
4
Medium Priority
?
454 Views
Last Modified: 2012-08-13
in a string, need to globally replace semicolons inside quotes, but leave the semicolons outside quotes.
important: there may be any number of semicolons inside one set of quotes.
thank you.

example

semicolon in smithers quote:

MrBurns:"One more thing...You must find the Jade Monkey before the next full moon!";Smithers:"Actually sir;we found the jade monkey;it was in your glove compartment.";MrBurns:"And the road maps, and the driving gloves?!"

smithers semicolon replaced:

MrBurns:"One more thing...You must find the Jade Monkey before the next full moon!";Smithers:"Actually sir~we found the jade monkey~it was in your glove compartment.";MrBurns:"And the road maps, and the driving gloves?!"
0
Comment
Question by:mtnr
  • 3
4 Comments
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16574246
Hi mtnr,

Sure, how does this look to you.  I tryed with regex and I could easily locate the sections but I could not replace that directly in the string.  So rather then make it needlessly complicated I just made this function.  I have another idea for another function that might be much shorter for now here it is:

<?php
// This function replaces semicolons with tildes
// inside double quotes
function semiColonoscopy($str)
{
  $pos = strpos($str, '"');
  $pos2 = strpos($str, '"', $pos+1);
  while ($pos != false AND $pos2 != false)
  {
    for ($i = $pos+1; $i < $pos2; $i++)
    {
      if ($str[$i] == ';')
         $str[$i] = '~';
    }
    $pos = strpos($str, '"', $pos2+1);
    if ($pos != false)
      $pos2 = strpos($str, '"', $pos+1);
  }
  return $str;
}

$str = 'MrBurns:"One more thing...You must find the Jade Monkey before the next full moon!";Smithers:"Actually sir;we found the jade monkey;it was in your glove compartment.";MrBurns:"And the road maps, and the driving gloves?!"';
print "Old:<br>$str<br>";
$str2 = semiColonoscopy($str);
print "New:<br>$str2";
?>

How is that =)
Joe P
0
 
LVL 17

Accepted Solution

by:
BogoJoker earned 2000 total points
ID: 16574267
There you go, much shorter:

<?php
// This function replaces semicolons with tildes
// inside double quotes
function semiColonoscopy($str)
{
  $inQuotes = false;
  for ($i = 0; $i < strlen($str); $i++)
  {
    if ($str[$i] == '"')
      $inQuotes = !$inQuotes;
    if ($str[$i] == ';' AND $inQuotes)
      $str[$i] = '~';
  }
  return $str;
}

$str = 'MrBurns:"One more thing...You must find the Jade Monkey before the next full moon!";Smithers:"Actually sir;we found the jade monkey;it was in your glove compartment.";MrBurns:"And the road maps, and the driving gloves?!"';
print "Old:<br>$str<br>";
$str2 = semiColonoscopy($str);
print "New:<br>$str2";
?>

More efficient too.  Just a boolean to denote when your inside double quotes.  When the boolean is enabled, replace semicolons with tildes, otherwise do nothing.  The boolean is flipped when you encounter the first double quote.
Joe P
0
 

Author Comment

by:mtnr
ID: 16574984
that's a neat approach. thank you!
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16574992
Sure, glad you liked it =)

Joe P
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question