Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:34:21+00:00 2026-06-10T06:34:21+00:00

This seems so obvious , but I cannot find a way to do this.

  • 0

This seems so obvious , but I cannot find a way to do this.
I think there even is a regular PHP function to do this, but even that one stays well hidden after 1,5 hours of intensive Google searches.

What I want

  • A function that takes a string as input.
  • Checks that string for the number of times it has alphabetical orderded sequences of more than 3 characters:
  • returns true if a sequence of more than 3 has been found.

Example

"youlookgreatbcdetoday" => has "bcde" in it … so has to return true
"youlookgreatklmtoday" => only has "klm" in it … so that has to return false
"youlookgreattoday" => has no alphabetically ordered sequences in it, so returns false

Possible usecase

  • password strength checker
  • wordgame
  • …

disclaimer: I wish I already had some code to show you, but I literally have nothing yet.
Only thing I could come up with was to split the string up in an array and do some magic on the array … but even then I got stuck.

Hope one of you will save me 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:34:23+00:00Added an answer on June 10, 2026 at 6:34 am

    So, let’s start off with a trivial implementation using a loop and a counter (for increasing only):

    function hasOrderedCharactersForward($string, $num = 4) {
        $len = strlen($string);
        $count = 0;
        $last = 0;
        for ($i = 0; $i < $len; $i++) {
            $current = ord($string[$i]);
            if ($current == $last + 1) {
                $count++;
                if ($count >= $num) {
                    return true;
                }
            } else {
                $count = 1;
            }
            $last = $current;
        }
        return false;
    }
    

    So, how does it work? Basically, it loops through, and checks to see if the ord (ascii number) of the character is one more than the one before it. If so, it increases the count parameter. Otherwise, it sets it to 1 (since we already processed that character). Then, if $count is ever more or equal to the number requested, we know we found a sequence, and can return…

    So, now let’s check in both directions:

    function hasOrderedCharacters($string, $num = 4) {
        $len = strlen($string);
        $count = 0;
        $dir = 1;
        $last = 0;
        for ($i = 0; $i < $len; $i++) {
            $current = ord($string[$i]);
            if ($count == 1 && $current == $last - 1) {
                $count++;
                $dir = -1;
                if ($count >= $num) {
                    return true;
                }
            } elseif ($current == $last + $dir) {
                $count++;
                if ($count >= $num) {
                    return true;
                }
            } else {
                $count = 1;
                $dir = 1;
            }
            $last = $current;
        }
        return false;
    }
    

    Now, it’ll return true for abcd and dcba…

    Now, here’s a far simpler solution:

    function hasOrderedCharactersForward($string, $num = 4) {
        $len = strlen($string) + 1;
        $array = array_map(
            function($m) use (&$len) {
                return ord($m[0]) + $len--;
            }, 
            str_split($string, 1)
        );
        $str = implode('_', $array);
        $regex = '#(^|_)(\d+)' . str_repeat('_\2', $num - 1) . '(_|$)#';
        return (bool) preg_match($regex, $str);
    }
    

    And there you go. We use the property that if we add a decreasing number to each position, consecutive sequences will appear as the same number. And that’s exactly how this works.

    And here’s the same theory applied to both directions:

    function hasOrderedCharacters($string, $num = 4) {
        $i = 0;
        $j = strlen($string);
        $str = implode('', array_map(function($m) use (&$i, &$j) {
            return chr((ord($m[0]) + $j--) % 256) . chr((ord($m[0]) + $i++) % 256);
        }, str_split($string, 1)));
        return preg_match('#(.)(.\1){' . ($num - 1) . '}#', $str);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems to be something obvious but I simply can't find info by googling.
This seems like a fairly obvious question, but I haven't been able to think
Hey, so this is one of those questions that seems obvious, and I'm probably
I apologize if the answer to this seems obvious but I just cannot figure
This seems like it should be blindingly obvious, but I cannot see the answer.
This seems like it should be obvious but I can't figure it out. Suppose
This seems like I'm missing something obvious but I can't get redirects (>) to
Seems like this should be obvious, but how do I send arrow key presses
This might sound like an obvious question but I can't seem to find the
Sorry if this is blatantly obvious, but I've Googled this and I seriously cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.