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.
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.
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):
Right... you sort of need to actually assign the trim(... to something to echo. Add $nom = in front of the trim-line
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):