I have a PHP regEx, how do add a condition for the number of characters?

前端 未结 5 1793
臣服心动
臣服心动 2021-01-27 02: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|o         


        
5条回答
  •  無奈伤痛
    2021-01-27 02:25

    Don't use a regex to break apart that path. Just use explode.

    $dirs = explode( '/', urldecode($path) );
    

    Then, if you need to break apart an individual element of the array, do that, like on your "pagerank-update" element at the end.

    EDIT:

    The key is that you have two different problems. First you want to break apart the path elements on slashes. Then, you want to break up the filename into smaller parts. Don't try to cram everything into one regex that tries to do everything.

    Three discrete steps:

    • $dirs = explode...
    • Weed out arguments < 3 chars
    • Break up file argument at the end

    It is far clearer if you break up your logic into discrete logical chunks rather than trying to make the regex do everything.

提交回复
热议问题