2013-11-21 48 views
0

我有我的應用程序的所有控制器列表的數組:如何將替換應用於PHP中的每個數組元素?

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

如何在每個數組元素一個PHP命令的端部條../controllers/開始和_controller.php

+0

顯示我們的輸入陣列和所述輸出陣列。現在不清楚你在問什麼。 – Achrome

+1

我認爲這很清楚,輸入數組將會是'array('../ controllers/test_controller.php','../ controllers/hello_controller.php');' – SamV

+0

就是這個數組! – rubo77

回答

1

作爲preg_replace函數可以在陣列上起作用,則可以這樣做:

$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); 

輸出:

Array 
(
    [0] => test 
    [1] => hello 
    [2] => user 
) 
0

如果你有一個這樣的數組:

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

然後array_map()幫你修剪不必要的字符串。

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

1

無需正則表達式在這種情況下,除非有可以爲你所提到的變化。

$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); 

這將輸出

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

這是(我認爲)你問什麼。

1

映射一個陣列到另一個:

​​
1

我不知道你怎麼定義「命令」,但我懷疑是有辦法做到這一點有一個簡單的函數調用。

但是,如果你只是想它是緊湊的,這裏做一個簡單的方法:

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

這是一個有點髒,但它可以在一個單一的線所做的工作。

+0

我喜歡它,那麼它可以只有兩個簡單的記住行:'$ controllerlist = str_replace('../ controllers /','',glob(「../ controllers/* _ controller.php」)); $ controllerlist = str_replace('_ controller.php','',$ controllerlist);' – rubo77

1

一個命令沒有搜索和替換?是的你可以!

如果我不是失去了一些東西大,怎麼樣使用SUBSTR函數保持簡單和斬波15個字符從一開始和結束:

SUBSTR($ X,15,-15)

由於glob總是會給你帶有該模式的字符串。

實施例:

// 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"); 
} 

輸出:

test 
hello 
user 
+0

你不能用['substr_replace']把它放在一行中(http://www.php.net/manual/en /function.substr-replace.php)? – rubo77

+0

我不知道,你試過了嗎? – zaf

+0

這是有效的:'$ array = substr_replace(substr_replace($ array,'', - 15),'',0.15)' – rubo77

0

preg_replace接受一個數組作爲參數也:

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

在一個命令剝離在開始15個字符和15在每個arrayelement的末尾:

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