Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

php \ escap special character problem

  • 02-07-2007 04:35PM
    #1
    Registered Users, Registered Users 2 Posts: 94 ✭✭


    Im trying to update an ms sql db but im strugglin with the pesky single quote. To update the db i need to covert a " ' " to " '' ". Then i try this i end up with a \. Really annoying.

    $storyfixed = eregi_replace("'", "''", $story);

    and


    $storyfixed = eregi_replace('\'', '\'\'', $story);

    are both producing the same problem

    "help me'o" gets transformed into:--> 1, 'help me\''o', 'blank', CURRENT_TIMESTAMP

    Anybody got any ideas?


Comments

  • Closed Accounts Posts: 30 Mr. Magoo


    This should do it

    $storyfixed = str_replace("'",'"',$story);


  • Registered Users, Registered Users 2 Posts: 94 ✭✭sinkingfish


    Thanks for the help, we were both close...

    this worked : $storyfixed = str_replace("\'","''",$story);

    A little bit of trial and error!


  • Registered Users, Registered Users 2 Posts: 568 ✭✭✭phil


    There's an addslashes() function in PHP which does this. You should be aware of SQL injection vulnerabilities you are opening yourself up to whenever you insert anything into an SQL database from user input fields.

    It's normally wiser to use some of the database abstraction libraries knocking around like adodb.


  • Registered Users, Registered Users 2 Posts: 804 ✭✭✭TimTim


    phil wrote:
    There's an addslashes() function in PHP which does this. You should be aware of SQL injection vulnerabilities you are opening yourself up to whenever you insert anything into an SQL database from user input fields.

    It's normally wiser to use some of the database abstraction libraries knocking around like adodb.

    While I don't claim to be an anyway decent php coder. I've read/heard using addslashes() and stripslashes() in a php application is just a plain stupid thing to do.

    If you are going to be using user input and putting it into a sql database mysql_real_escape_string() would better thing to use.


  • Registered Users, Registered Users 2 Posts: 1,393 ✭✭✭Inspector Gadget


    I'd suggest the adodb library too (it's very handy, in my opinion) - it's got a method called qstr() that does exactly this.

    Hope this helps,
    Gadget


  • Advertisement
Advertisement