Can PHP identify identify uppercase letters in a string?

7 replies
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
#identify #letters #php #string #uppercase
  • Profile picture of the author stma
    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.
    {{ DiscussionBoard.errors[1994355].message }}
    • Profile picture of the author theIMgeek
      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
      Signature
      FREE WSO: Protect and Automatically Deliver Your Digital Products

      Ask the Internet Marketing Geek
      <-- Happy to help with technical challenges
      MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
      {{ DiscussionBoard.errors[1994454].message }}
      • Profile picture of the author Stephen Dean
        Amazing, thanks a ton for your help Ryan. (And thanks for giving it a try, Ed.) I have one more problem, but I might be able to figure it out now that you've pointed me in the right direction.

        HOW could I make sure the beginning of a sentence stays capitalized if need be? Like in this string?

        Before I knew your answer to the last problem I was thinking of just reversing the case. tHAT way if you reversed the case of "THAT" at the beginning of this sentence, it would display correctly. Not sure if that's the best way to do it.

        In any case, I might be able to figure it out based on your help. I do appreciate it. I'll send you both a copy of the software when ready - it's pretty much done except for this.

        Cheers,
        Stephen Dean
        Signature
        Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

        Occupation: Best Copywriter Ever.
        Clients:
        Matt Bacak, Jim Edwards, Ryan Deiss and more.
        {{ DiscussionBoard.errors[1994792].message }}
  • Profile picture of the author theIMgeek
    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
    Signature
    FREE WSO: Protect and Automatically Deliver Your Digital Products

    Ask the Internet Marketing Geek
    <-- Happy to help with technical challenges
    MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
    {{ DiscussionBoard.errors[1996554].message }}
    • Profile picture of the author Stephen Dean
      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?
      Signature
      Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

      Occupation: Best Copywriter Ever.
      Clients:
      Matt Bacak, Jim Edwards, Ryan Deiss and more.
      {{ DiscussionBoard.errors[1996967].message }}
  • Profile picture of the author theIMgeek
    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
    Signature
    FREE WSO: Protect and Automatically Deliver Your Digital Products

    Ask the Internet Marketing Geek
    <-- Happy to help with technical challenges
    MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
    {{ DiscussionBoard.errors[1997335].message }}
  • Profile picture of the author Stephen Dean
    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
    Signature
    Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

    Occupation: Best Copywriter Ever.
    Clients:
    Matt Bacak, Jim Edwards, Ryan Deiss and more.
    {{ DiscussionBoard.errors[1998940].message }}

Trending Topics