How to remove scripts from a text string in PHP?

1

I have a question,

  

How can I delete the - scripts from a text string?

I did the following but it does not work for me.

$cadena = "Mi-cadena-con-guiones"; 
$cadena = preg_replace('[\s+]',"", $cadena);
    
asked by Jhosselin Giménez 22.04.2017 в 04:57
source

2 answers

6

To be able to delete the scripts you can do it this way:

str_replace("-","",$cadena);

What it will do is look for the scripts of that chain and replace them for whatever you like, in the example I left it "" the result will be this:

Micadenaconespacios
    
answered by 22.04.2017 / 04:59
source
2

The common way is to use str_replace() :

$cadena = "Mi-cadena-con-guiones"; 
$str = str_replace("-", " ", $cadena);
echo $str;

but another option is to use explode() and implode() :

$str = "Mi-cadena-con-guiones";
$arr = explode("-",$string);
$str = implode(" ",$arr);
echo $str;

both options will result in:

  Mi cadena con guiones

You decide if you want to replace the - for a space " " or an empty space "" .

    
answered by 22.04.2017 в 05:05