0

my title consists of space,[ { } & () capital letters and several other unknown characters which i want to replace to lower character only. i am using this but as i cant predict what users will give input i am considering some sort of regex or preg replace or preg match so that only lower letters with underscore(for space) is acceptable or title is converted into all lower case with underscore.i dont want any other character like ,{[]}() & etc.

i have tried this but will prefer one line preg replace or regex

     $name = str_replace(' ', '_', $cityu);
     $name1 = str_replace('(', '_', $name);
     $name2 = str_replace(')', '_', $name1);
wayenjoy
  • 71
  • 1
  • 10
  • 1
    The joys: http://php.net/strtolower :) It won't mess up other characters than A-Z –  Mar 11 '13 at 17:23
  • @Allendar what about special character ??????? – wayenjoy Mar 11 '13 at 17:24
  • It will just comply to the ISO Latin typeset and replace it with it's lower-case version (if it exists of course). I've read on the doc-page on php.net some special language letters like Polish and Scandinavian might fail. You really need to do some testing on those characters and make exceptions. Maybe there is already an API in existence for these type of occasions, but sadly none that I know of with 100% accuracy. –  Mar 11 '13 at 17:25
  • This caught my attention for special characters, I hope it's of use: http://www.astanos.ch/en/blog/6-php-strtolower-and-utf-8.html Good luck! –  Mar 11 '13 at 17:31
  • http://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string – Curlas Mar 11 '13 at 17:44

3 Answers3

5

I think you can use something like this :

$string = preg_replace('/[^\w]/','_', $string);
$string = strtolower($string);
Skydiver
  • 403
  • 2
  • 11
0

For preg_replace() you want to use a character class which is enclosed in square brackets [], eg:

$output = preg_replace('/[() ]/', '_', $input);

however, if you want all non-alphabet characters replaced, then you can use:

$output = preg_replace('/[^a-z0-9]/', '_', $input);

Where ^ inside a character class means "not", so it replaces characters that are not from a to z or 0 to 9.

Lastly you can convert to lower case simply with strtolower(), so your code would look like:

$output = preg_replace('/[^a-z0-9]/', '_', strtolower($input));

You might also want to condense multiple ____ into one _, which can be done by:

$output = preg_replace('/_{2,}/', '_', $input);

Where {2,} indicates to match two or more occurences of the _ character.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
0

There is some level of ambiguity in your question requirements, so I'll cover a few scenarios. If you need to accommodate for unicode characters, you should clarify that detail in your question via an edit.

Code: (Demo)

$string = "(Here {is [a] Special} Title)! & it's fine, right?";

// all non-letters become underscores
echo preg_replace('~[^a-z]~', '_', strtolower($string));

echo "\n\n";
// notice the consecutive underscores
echo str_replace(' ', '_', preg_replace('~[^a-z ]+~', '', strtolower($string)));

echo "\n\n";
// notice no consecutive underscores
echo preg_replace(['~[^a-z ]+~', '~ +~'], ['', '_'], strtolower($string));

Output:

_here__is__a__special__title_____it_s_fine__right_

here_is_a_special_title__its_fine_right

here_is_a_special_title_its_fine_right

The first technique replaces each non-letter character with an underscore individually.

The second technique removes consecutive sequences of non-letters&non-space characters, then replaces each space individually with an underscore.

The third technique removes consecutive sequences of non-letter&non-space characters, then replaces consecutive sequences of spaces with an underscore.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136