Search code examples
phplowercase

PHP Ucwords with or and special characters


Here is what I'm doing.

I have a couple of strings that is uppercase

†HELLO THERE
DAY OR NIGHT

So to convert them, I'm using the following code:

ucwords(strtolower($string));

Here is the end result:

†hello There
Day Or Night

How can I ignore the or any special characters so it the words can show

†Hello There

and how can I keep words like or all lowercase.


Solution

  • Try:

    print preg_replace_callback('#([a-zA-ZÄÜÖäüö0-9]+)#',function($a){
       return ucfirst(strtolower($a[0]));
     },
     '†hello THERE'
    );
    

    [a-zA-ZÄÜÖäüö0-9]+ find a word that only has this chars

    You can also use this instead [\w]+ see: http://www.regular-expressions.info/wordboundaries.html

    preg_replace_callback call a function on the found result

    function($a){} do something with the result, here ucfirst(strtolower())