Reputation: 3440
I try to combine strtolower
and preg_replace
, but I am not sure how to nest it. I have tried it like so:
$res = strtolower(preg_replace("/[^a-zA-Z]/", "", $string));
I found this solution but cannot get it to work. I want a lowercase string without special characters:
I am some special-content
turns into
iamsomespecialcontent
How can I achieve this and store it inside $res
?
Upvotes: 0
Views: 384
Reputation: 787
Well I can say first step will be making string as lowercase
$rest = strtolower($string);
And then removing white spaces
$rest = preg_replace("/\s+/", "", $rest);
You can combine it as -
$rest = preg_replace("/\s+/", "", strtolower($rest));
For more special character related solution you can try this
Upvotes: 1
Reputation: 41776
Its better to split this into a two-liner and debug the output of the commands with var_dump() in order to see whats going on:
<?php
/* string with special chars */
$string = 'abczABCZ-#+´!"§123';
$no_special_chars = preg_replace("/[^a-zA-Z]/", "", $string);
var_dump($no_special_chars); // string 'abczABCZ' (length=8)
$lowercased = strtolower($no_special_chars);
var_dump($lowercased); // string 'abczabcz' (length=8)
And maybe you noticed, that you don't have to handle A-Z
in the preg_replace(), if you lowercase the string first.
$res = preg_replace("/[^a-z]/", "", strtolower($string));
var_dump($res); // string 'abczabcz' (length=8)
Upvotes: 1