0

I have an array with a list of all controllers in my application:

$controllerlist = glob("../controllers/*_controller.php");

How do I strip ../controllers/ at the start and _controller.php at the end of each array element with one PHP command?

2
  • Show us the input array and the output array. Right now it is unclear what you are asking. Commented Nov 21, 2013 at 10:51
  • 1
    I thought it was quite clear, the input array would be something like array('../controllers/test_controller.php', '../controllers/hello_controller.php'); Commented Nov 21, 2013 at 10:52

8 Answers 8

2

As preg_replace can act on an array, you could do:

$array = array(
    "../controllers/test_controller.php",
    "../controllers/hello_controller.php",
    "../controllers/user_controller.php"
);

$array = preg_replace('~../controllers/(.+?)_controller.php~', "$1", $array);
print_r($array);

output:

Array
(
    [0] => test
    [1] => hello
    [2] => user
)
Sign up to request clarification or add additional context in comments.

Comments

1

Mapping one array to another:

$files = array(
  '../controllers/test_controller.php',
  '../controllers/hello_controller.php'
);
$start = strlen('../controllers/');
$end = strlen('_controller.php') * -1;

$controllers = array_map(
  function($value) use ($start, $end) {
    return substr($value, $start, $end);
  },
  $files
);
var_dump($controllers);

Comments

1

I'm not sure how you defined "command", but I doubt there is a way to do that with one simple function call.

However, if you're simply wanting it to be compact, here's a simple way of doing it:

$controllerlist = explode('|||', str_replace(array('../controllers/', '_controller.php'), '', implode('|||', glob("../controllers/*_controller.php"))));

It's a bit dirty, but it gets the job done in a single line.

1 Comment

I like it, then it can be only two simple to remember lines: $controllerlist = str_replace('../controllers/', '', glob("../controllers/*_controller.php")); $controllerlist = str_replace('_controller.php', '', $controllerlist);
1

One command without searching and replacing? Yes you can!

If I'm not missing something grande, what about keeping it simple and chopping 15 characters from the start and the end using the substr function:

substr ( $x , 15 , -15 )

Since glob will always give you strings with that pattern.

Example:

// test array (thanks FruityP)
$array = array(
    "../controllers/test_controller.php",
    "../controllers/hello_controller.php",
    "../controllers/user_controller.php" );

foreach($array as $x){ 
   $y=substr($x,15,-15); // Chop 15 characters from the start and end
   print("$y\n");
}

Output:

test
hello
user

2 Comments

can't you put that in one line with substr_replace?
This works: $array = substr_replace(substr_replace($array,'',-15),'',0,15)
1

No need for regex in this case unless there can be variations of what you mentioned.

$array = array(
    "../controllers/test_controller.php",
    "../controllers/hello_controller.php",
    "../controllers/user_controller.php"
);

// Actual one liner..
$list = str_replace(array('../controllers/', '_controller.php'), "", $array);

var_dump($array);

This will output

array (size=3)
  0 => string 'test' (length=4)
  1 => string 'hello' (length=5)
  2 => string 'user' (length=4)

Which is (I think) what you asked for.

Comments

0

If you have an array like this :

$array = array(  "../controllers/*_controller.php", 
                  "../controllers/*_controller.php");

Then array_map() help you to trim the unnecessary string.

function trimmer( $string ){
    return str_replace( "../controllers/", "", $string );
 }
 $array = array(  "../controllers/*_controller.php", 
                  "../controllers/*_controller.php");

 print_r( array_map( "trimmer", $array  ) );

http://codepad.org/VO6kyVOa

Comments

0

to strip 15 chars at the start and 15 at the end of each arrayelement in one command:

$controllerlist = substr_replace(
     substr_replace(
          glob("../controllers/*_controller.php"),'',-15
          ),'',0,15
      )

Comments

0

preg_replace accepts an array as argument too:

$before = '../controllers/';
$after = "_controller.php";
$preg_str = preg_quote($before,"/").'(.*)'.preg_quote($after,"/");
$controllerlist = preg_replace('/^'.$preg_str.'$/', '\1',  glob("$before*$after"));

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.