Can PHP identify identify uppercase letters in a string?

by 7 replies
10
I'm trying to find uppercase letters in a string and make them bold (meaning, put html tags around them <b></b>). Is that possible?

And really I only want this to happen when it's SEVERAL uppercase letters in a row.

So the sentence above would now look like...

"And really I only want this to happen when it's several uppercase letters in a row."

Thanks for the help.

Cheers,
Stephen Dean
#programming #identify #letters #php #string #uppercase
  • You should be able to do this with regular expressions - I just tried a few different ways and can't seem to make it work though. I'm by no means good at regex though.
    • [1] reply
    • I must say I learned a little something about PHP inside regular expressions. Nifty!

      Here it is. Tested and working! The {3,} means it will replace any word of 3 or more consecutive capital letters.


      $string = "And really I only want this to happen when it's SEVERAL uppercase letters in a row.";
      $pattern = '/([A-Z0-9]{3,})/e';
      $replacement = "'<strong>'.strtolower('\\1').'</strong>'";
      $formatted_string = preg_replace($pattern, $replacement, $string);
      echo $formatted_string;

      -Ryan
      • [1] reply
  • Banned
    [DELETED]
  • Hi Stephen. You're welcome.

    I'm unclear on this second requirement.

    WHEN a sentence starts with all capitals, should they remain untouched? Or should they be made bold but remain uppercase?

    Please lay out the specific rules for what you need to happen in what circumstances.

    -Ryan
    • [1] reply
    • Thanks again Ryan.

      1. When all caps is found, the caps should be turned to lower case with <strong> tags surrounding.
      2. If a lower case letter immediately proceeds the caps block (and not a space), bold it as well but don't reverse it's case.

      That's my attempt at rules, anyway. Hope that's clear. I'm trying to fix the problem that would occur if the user used all caps at the beginning of a sentence. It would leave the first letter of the sentence uncapitalized. For example,

      Where THIS SENTENCE would be ok.

      THIS SENTENCE would start with a lower case letter when sentences are supposed to start with an upper case.

      Currently for the example above the output would look like this:

      "this sentence would start with..."

      And to read correctly it should be:

      "This sentence would start with..."

      I was thinking the user could signify it's the start of a sentence by having the first letter in lower case, followed by all upper case.

      tHIS sentence would start with a lower case, then caps in the first word.

      And that would turn in to:

      "This sentence would start with..."

      Does that make sense?
  • Secretly, it brings me joy when I'm able to tame the mighty regex dragon.

    This should do a pretty good job of finding CAPS at the beginning of sentences.

    $string = "FROM the beginning, this is a TEST. HOPEFULLY it will work.
    And really I only want this to happen when it's SEVERAL uppercase letters in a row.
    ALSO with line breaks.";
    $pattern = array('/(^|[.?!][\s]+)([-A-Z0-9]{3,})/e','/([-A-Z0-9]{3,})/e');
    $replacement = array("'\\1<strong>'.strtoupper(substr('\\2',0,1)) .strtolower(substr('\\2',1)).'</strong>'","'<strong>'.strtolower('\\1').'</strong>'");
    echo preg_replace($pattern, $replacement, $string);

    -Ryan
    • [ 1 ] Thanks
  • Thanks Ryan, that's a ton of help. Soon as I release this software I'll send it over to you, hopefully soon. You just made it a lot better.

    Cheers,
    Stephen

Next Topics on Trending Feed