Fou
Fou

Reputation: 916

PHP how to remove all other operators except -

i want to remove all other operators except -. i worked on the following but stuck here.

$s1 = "Hello@*&^%$#@!)({}[]?.,<> dis$ is. testing -";
$s2 = "- i love% the way. it, works";

for($ii=0;$ii<3;$ii++){

$from = array('/\(|\)/','/\d+ml|\d+g/','/\s+/'); 
$to = array('','','-'); 

$sample=${'s' . $ii}; 

$sample = strtolower(trim(preg_replace($from,$to,$sample),'-')); 
echo $sample."<br>";

}

please help me in this regards.

Output required

hello-dis-is-testing
i-love-the-way-it-works

Upvotes: 0

Views: 76

Answers (1)

karka91
karka91

Reputation: 733

$sample = trim(preg_replace('/\\W+/i', '-', $s1), '-');

This regex replaces everything except letters and numbers with a dash and trim removes trailing and preceding dashes

Upvotes: 1

Related Questions