Results 1 to 5 of 5

Thread: several regex from post

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default several regex from post

    i have an ajax form posting into a sql db and would like to backslash single, double quotes and the backslash itself, this doesnt seem to work from a <input /> field

    Code:
    $old = $_POST['title'];
    
    $newTitle = trim( preg_replace('/\\\\/','\\\\\\\\',$old) );	// backslash a backslash \\
    $newTitle = trim( preg_replace( '/[\"]/', '\"', $old ) );		// backslash the double quotes \"
    $newTitle = trim( preg_replace( '/[\']/', '\"', $old ) );		// backslash the single quotes \'
    
    mysql_query("UPDATE `mytable` SET title ='$newTitle' WHERE id='$id'");
    would i have to turn all those regex into 1 concise statement?
    Last edited by ggalan; 12-22-2011 at 12:05 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    No. Perhaps you could, perhaps not, or there might be a PHP function or functions that do all those things even more concisely. If you don't though, you would have to chain them. Here's one way:

    Code:
    $old = $_POST['title'];
    
    $newTitle = trim( preg_replace('/\\\\/','\\\\\\\\',$old) );	// backslash a backslash \\
    $newTitle = trim( preg_replace( '/[\"]/', '\"', $newTitle ) );		// backslash the double quotes \"
    $newTitle = trim( preg_replace( '/[\']/', '\"', $newTitle ) );		// backslash the single quotes \'
    
    mysql_query("UPDATE `mytable` SET title ='$newTitle' WHERE id='$id'");
    But I think addslashes():

    http://www.php.net/manual/en/function.addslashes.php

    will do it all for you in one pass.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    when i create a file to output the result it almost works, but the single quote is coming out as a double
    this was my input

    / \ ' "

    Code:
    $File = "test.txt"; 
    $Handle = fopen($File, 'w');
    $Data = "$newTitle"; 
    fwrite($Handle, $Data); 
    fclose($Handle);
    output from text file:
    Code:
    / \\ \" \"
    but in mysql i dont get the backslashes and the single quote comes out as double
    Code:
    / \ " "

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's probably something in your regular expression(s). Have you tried addslashes? It's designed for entering data into a database, specifically to escape the \, ' , and " characters. Assuming the rest of your code is right, I think it would go like:

    PHP Code:
    $old $_POST['title'];

    $newTitle trimaddslashes($old) );    // add slashes

    mysql_query("UPDATE `mytable` SET title ='$newTitle' WHERE id='$id'"); 
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ggalan (12-22-2011)

  6. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    thank you, that did it. my problem was in the html output. is there a php function that can turn these slashes into a format that is displayable?
    Code:
    <input class='textfield' type='text' title='title' value='a \' \" / \\ v' />
    i tried htmlspecialchars() but it doesnt take care of the single quote



    re: this did it in the html output
    Code:
    $newTitle = htmlspecialchars($title, ENT_QUOTES);
    Last edited by ggalan; 12-22-2011 at 12:04 AM.

Bookmarks

Posting Permissions

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