1

I'm basically trying to extract parts of a string AFTER a character "/" but using PHP PCRE (Regular Expressions) NOT PHP substr() function, I would like to test if the initial string has multiple "/" characters using a combination of PHP PCRE (Regular Expressions) and preg_match() or preg_match_all().

I am able to select for a SINGLE iteration using a regular expression.

<?php
  $rules = array(
    'dbl'     => "/(?'d'[^/]+)/(?'p'[^/]+)", // '.../a/a' DOUBLE ITERATION
    'single'     => "/(?'d'[\w\-]+)",// '.../a' SINGLE ITERATION
    'multiple'     => "" //MULTIPLE ITERATION
  );
  $string = "a/b/c/d/e";
  foreach ( $rules as $action => $rule ) {
    if ( preg_match_all( '~^'.$rule.'$~i', $string, $params ) ) {
      switch ($action) {
        case 'multiple':
          $arr = explode("/", $string);
          print_r($arr);
          //do something
        ...
      }
    }
  }


?>

I know this is because of my lack of sufficient knowledge of Regular Expressions, however, I need a dynamic Regex code to match the condition that the initial string has multiple "/" characters and then recursively store these substrings to an array.

7
  • 1
    Something like if(preg_match('~^[^/]+(?:/[^/]+)+/?$~', $string)) { print_r(explode('/', $string)); }? If you want to check for at least 2 /s, replace the last + with {2,} Commented Jul 24, 2019 at 12:12
  • I would approach this differently: I would first explode $string on / and then apply logic based on the number of elements in the results. Commented Jul 24, 2019 at 12:25
  • Hi @WiktorStribiżew, Yes, thats exactly what i needed. Kindly post your answer so that i may accept it. Commented Jul 24, 2019 at 12:28
  • Hi @joanis, Please explain further. Commented Jul 24, 2019 at 12:30
  • I just wanted to make sure I understand the final result. @joanis idea is better, you do not actually need a regex here. Commented Jul 24, 2019 at 12:33

2 Answers 2

0

I would approach this differently: I would first explode $string on / and then apply logic based on the number of elements in the results.

<?php
  $string = "a/b/c/d/e";
  $arr = explode("/", $string);
  if (count($arr) > 2) {
    print_r($arr);
    // do something knowing there were 2 or more slashes in $string
  }
?>

If you need different actions for 0, 1 or 2 slashes, add elseif blocks testing for fewer elements in $arr and put the corresponding actions there.

Sign up to request clarification or add additional context in comments.

2 Comments

This works but the aim was using Regexes to handle this as stated in the question.
Now I am curious, what is your use case for absolutely needing to solve this with a regex?
0

To answer the question, Using Wiktor Stribiżew's Regex Code:

<?php
  $rules = array(
    'dbl'     => "/(?'d'[^/]+)/(?'p'[^/]+)", // '.../a/a' DOUBLE ITERATION
    'single'     => "/(?'d'[\w\-]+)",// '.../a' SINGLE ITERATION
    'multiple'     => "/[^/]+(?:/[^/]+){2,}/?" //MULTIPLE ITERATION
  );
  $string = "a/b/c/d/e";
  foreach ( $rules as $action => $rule ) {
    if ( preg_match_all( '~^'.$rule.'$~i', $string, $params ) ) {
      switch ($action) {
        case 'multiple':
          $arr = explode("/", $string);
          print_r($arr);
          //do something
        ...
      }
    }
  }


?>

For others who reference this resource, kindly upvote Wiktor Stribiżew's answer once/ if he posts it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.