Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    eNYCe
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regex to replace \" with " and replace " with ' (code cleaning after bad programmer)

    I am cleaning a previous programmers work and am attempting to replace occurences like this:

    echo "<form action=\"myform.php\" method=\"post\"><input type=\"Hidden\" name=\"Price\" value=\"".$product["price"]."\">";

    Which is just ridiculous to try and make HTML adjustments to.

    I would like to make a regex that replaces all occurences of " with ' except when a \ precedes it, in which case it should just drop the slash and leave the quote.

    \" becomes "
    " becomes '

    but \" should not become '

    I thought of replacing all \" with some arbitrary unicode charector that would never appear in the code and then replacing " with ' and finally from unicode charctor to " but there must be a better way with regex.

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts

    You could try...

    stripslashes( $code )

    str_replace('\"', '"', $code);

    I dont really understand, but this may help...

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, this is going to be a backslash desaster if vBulletin intends to strip them away from my post. Anyway, let's try it:

    Code:
    $search = array(
    	'/(?<!\\\\)"/',
    	'/\\\\"/'
    );
    
    $replace = array(
    	"'",
    	'"'		
    );
    
    print preg_replace($search, $replace, $file);
    Hmh... looks okay in the preview mode... submitting...

  • #4
    New Coder
    Join Date
    Sep 2002
    Location
    eNYCe
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much, I needed to modify it slightly to work with a form result and display in the browser, but otherwise, great:
    Code:
    if(isset($code)) {
    $code=str_replace("'","\\'",stripslashes($code));
    $search = array(
    	'/(?<!\\\\)"/',
    	'/\\\\"/'
    );
    $replace = array(
    	"'",
    	'"'		
    );
    echo str_replace("\n",'<br>',htmlspecialchars(preg_replace($search, $replace, $code)));
    } else {
    echo '<form><textarea cols=120 rows=40 name="code"></textarea><br><input type="submit"></form>';
    }
    uggg, here is something that is the remnants of previous manual attempts at conversion with an obvious oversight:

    echo '<area coords="65,1,35,22" href="products.php?section=<?= $section ?>&sort=price">';

    Any way to replace it with concat strings like

    products.php?section='.$section.'&sort

    There are so many variations I dunno if it is reasonable even, specifically because the previous programmers have no spacing consistancy, there appears all of the below:

    <?= $section ?>
    <?=$section?>
    <?= $section; ?>
    <?echo $section; ?>
    <?echo $section?>

    you get the idea...

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure that's possible

    Code:
    print preg_replace(
    	'/\<\?(\s*?echo|\=\s*?)\s*(.+?)\s*;?\s*\?\>/', 
    	"' . \\2 . '", 
    	$file
    );
    I am used to seperate string operators with a space (whitespace is good for your eyes), that's why I tailored the RegExp this way.

  • #6
    New Coder
    Join Date
    Sep 2002
    Location
    eNYCe
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks dude, your pretty good at regex!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •