1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Replace strange characters

Discussion in 'PHP' started by piropeator, Jun 7, 2016.

  1. #1
    I have this code:
    $sth = $BD->prepare("INSERT INTO table (codigo, nom) VALUES (:codigo, :nom)");
    $codigo = "666000";
    $nom   = "  mac'p_i¥At"-o   ";
    
    $sth->bindParam(':codigo', $codigo);
    $sth->bindParam(':nom', $nom);
    
    $sth->execute();
    PHP:
    I want to clean my var $nom="macpiñato". First delete blank spaces before and after of value.
    Delete too (') and (") and underline (_) and line (-), etc.
    How I do that. I read about trim(), str_replace, preg_replace, etc.
     
    piropeator, Jun 7, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Basically, you can do something like this:
    
    $replace_these = ['\'','"','_','-'];
    $replace_with = [' ',' ',' ',' '];
    trim(str_replace($replace_these,$replace_with,$nom));
    
    Code (markup):
    That should do it.
     
    PoPSiCLe, Jun 7, 2016 IP
  3. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #3
    I used that code:
    <?php
        $nom= "  'roberto-sanchez   ";
        $replace_these = ['\'','"','_','-'];
        $replace_with = [' ',' ',' ',' '];
        trim(str_replace($replace_these,$replace_with,$nom));
        echo $nom;
    ?>
    PHP:
    The exist is:
    'roberto-sanchez
    Code (markup):
    and I want
    roberto sanchez
    Code (markup):
     
    piropeator, Jun 7, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Right... you sort of need to actually assign the trim(... to something to echo.
    Add $nom = in front of the trim-line
     
    PoPSiCLe, Jun 7, 2016 IP
  5. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #5
    This run OK. Is easy:
    $sql = "UPDATE xxx SET nom = trim(nom)";
        $sth = $BD->prepare($sql);
        $sth->execute();
    
    PHP:
    But this:
    $replace_these = ['"','\''];
    $replace_with = ['',''];
    $sql = "UPDATE xxx SET stud = trim(str_replace($replace_these, $replace_with, $stud)";
    $sth = $BD->prepare($sql);
    $sth->execute();
    PHP:
    The line of UPDATE show this message:
    Notice: Array to string conversion in
    Code (markup):
    :(
     
    piropeator, Jun 8, 2016 IP