Page 1 of 1

How to get capital letters from a string?

Posted: Wed Jul 21, 2004 7:51 pm
by hawleyjr
What is the best way to get the capital letters from a string?


ex:

Code: Select all

<?php

$myString = 'HereIsMyStringMcDonald';

echo getCapitalLetters($myString );

//Would return:  HIMSMD


function getCapitalLetters($str){

        //extract capital letters and return
}
?>

Posted: Wed Jul 21, 2004 7:53 pm
by feyd
untested:?:

Code: Select all

function getCapitalLetters($str)
{
  if(preg_match_all('#([A-Z]+)#',$str,$matches))
    return implode('',$matches[1]);
  else
    return false;
}

Posted: Wed Jul 21, 2004 7:58 pm
by hawleyjr
Works awesome.

Thanks Feyd. I've noticed your typically the first to respond with regular expressions. Is this something you learned programming other languages?

Posted: Wed Jul 21, 2004 8:02 pm
by feyd
Not really.. I just have some decent practice with them ;) They're my kinda language.. :D