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 9019691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:50:32+00:00 2026-06-16T04:50:32+00:00

I have a regular expression that Im using in php: $word_array = preg_split( ‘/(\/|\.|-|_|=|\?|\&|html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|%|\+)/’,

  • 0

I have a regular expression that Im using in php:

$word_array = preg_split(
    '/(\/|\.|-|_|=|\?|\&|html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|%|\+)/',
    urldecode($path), NULL, PREG_SPLIT_NO_EMPTY
);

It works great. It takes a chunk of url paramaters like:

/2009/06/pagerank-update.html

and returns an array like:

array(4) {
  [0]=>
  string(4) "2009"
  [1]=>
  string(2) "06"
  [2]=>
  string(8) "pagerank"
  [3]=>
  string(6) "update"
}

The only thing I need is for it to also not return strings that are less than 3 characters. So the "06" string is garbage and I’m currently using an if statement to weed them out.

  • 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-16T04:50:33+00:00Added an answer on June 16, 2026 at 4:50 am

    The magic of the split. My original assumption was technically not correct (albeit a solution easier to come to). So let’s check your split pattern:

    (\/|\.|-|_|=|\?|\&|html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|%|\+)
    

    I re-arranged it a bit. The outer parenthesis is not necessary and I moved the single characters into a character class at the end:

     html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|[\/._=?&%+-]
    

    That for some sorting upfront. Let’s call this pattern the split pattern, s in short and define it.

    You want to match all parts that are not of those characters from the split-at pattern and at minimum three characters.

    I could achieve this with the following pattern, including support of the correct split sequences and unicode support.

    $pattern    = '/
        (?(DEFINE)
            (?<s> # define subpattern which is the split pattern
                html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|
                [\\/._=?&%+-] # a little bit optimized with a character class
            )
        )
        (?:(?&s))          # consume the subpattern (URL starts with \/)
        \K                 # capture starts here
        (?:(?!(?&s)).){3,} # ensure this is not the skip pattern, take 3 characters minimum
    /ux';
    

    Or in smaller:

    $path       = '/2009/06/pagerank-update.htmltesthtmltest%C3%A4shtml';
    $subject    = urldecode($path);
    $pattern    = '/(?(DEFINE)(?<s>html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|[\\/._=?&%+-]))(?:(?&s))\K(?:(?!(?&s)).){3,}/u';
    $word_array = preg_match_all($pattern, $subject, $m) ? $m[0] : [];
    print_r($word_array);
    

    Result:

    Array
    (
        [0] => 2009
        [1] => pagerank
        [2] => update
        [3] => test
        [4] => testä
    )
    

    The same principle can be used with preg_split as well. It’s a little bit different:

    $pattern = '/
        (?(DEFINE)       # define subpattern which is the split pattern
            (?<s>
        html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|
        [\/._=?&%+-]
            )
        )
        (?:(?!(?&s)).){3,}(*SKIP)(*FAIL)       # three or more is okay
        |(?:(?!(?&s)).){1,2}(*SKIP)(*ACCEPT)   # two or one is none
        |(?&s)                                 # split @ split, at least
    /ux';
    

    Usage:

    $word_array = preg_split($pattern, $subject, 0, PREG_SPLIT_NO_EMPTY);
    

    Result:

    Array
    (
        [0] => 2009
        [1] => pagerank
        [2] => update
        [3] => test
        [4] => testä
    )
    

    These routines work as asked for. But this does have its price with performance. The cost is similar to the old answer.

    Related questions:

    • Antimatch with Regex
    • Split string by delimiter, but not if it is escaped

    Old answer, doing a two-step processing (first splitting, then filtering)

    Because you are using a split routine, it will split – regardless of the length.

    So what you can do is to filter the result. You can do that again with a regular expression (preg_filter), for example one that is dropping everything smaller three characters:

    $word_array = preg_filter(
        '/^.{3,}$/', '$0', 
        preg_split(
            '/(\/|\.|-|_|=|\?|\&|html|shtml|www|php|cgi|htm|aspx|asp|index|com|net|org|%|\+)/',
            urldecode($path), 
            NULL, 
            PREG_SPLIT_NO_EMPTY
        )
    );
    

    Result:

    Array
    (
        [0] => 2009
        [2] => pagerank
        [3] => update
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a regular expression in PHP that looks for the date in the
I have a regular expression that is used to re-define a constant in php
I have this HTML that I am reading from wordpress into a regular PHP
I have created a Regular Expression (using php) below; which must match ALL terms
I have a regular expression to match usernames (which functions in PHP using preg_match
I have this Regular Expression that matches the following strings: <!-- 09-02-2009 ---> <!--
How can I have a regular expression that tests for spaces or tabs, but
I have a Java regular expression that captures stack exceptions from a string: ((?s).+(?:Exception|Error)[^\n]++(?:\s+at
I have a Python regular expression that matches a set of filenames. How to
I have the following regular expression that works fine in perl: Classification:\s([^\n]+?)(?:\sRange:\s([^\n]+?))*(?:\sStructural Integrity:\s([^\n]+))*\n The

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.