Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何用数组中的一些值填充图案_Php_Regex - Fatal编程技术网

Php 如何用数组中的一些值填充图案

Php 如何用数组中的一些值填充图案,php,regex,Php,Regex,我有一个uri模式,我想用一些值填充它。 我只有这两个变量 $pattern = "/^(?P<scope>[a-zA-Z]+)\/(?P<node>[a-zA-Z]+)\/(?P<controller>[a-zA-Z]+)\/(?P<action>[a-zA-Z]+)$/"; $replacement = array('scope'=>'Modules', 'node'=>'Index', 'controller'=>'Ind

我有一个uri模式,我想用一些值填充它。 我只有这两个变量

$pattern = "/^(?P<scope>[a-zA-Z]+)\/(?P<node>[a-zA-Z]+)\/(?P<controller>[a-zA-Z]+)\/(?P<action>[a-zA-Z]+)$/";

$replacement = array('scope'=>'Modules', 'node'=>'Index', 'controller'=>'Index', 'action'=>'index');
$pattern=“/^(?P[a-zA-Z]+)\/(?P[a-zA-Z]+)\/(?P[a-zA-Z]+)\/(?P[a-zA-Z]+)$/”;
$replacement=array('scope'=>'Modules','node'=>'Index','controller'=>'Index','action'=>'Index');
我正在寻找一种获得这个输出的方法:Modules/Index/Index/Index


谢谢大家!

这可能很有用,这取决于您的URI,尽管它使用PHP的str_replace而不是regex

$oldURI  = 'blah/<scope>/<node>/<controller>/<action>/';
$search  = array('<scope>','<node>','<controller>','<action>');
$replace = array('Modules','Index','Index','index');
$newURI  = str_replace($search, $replace, $oldURI);
$oldURI='blah//';
$search=数组(“”,“”,“”);
$replace=array('Modules','Index','Index','Index');
$newURI=str_replace($search,$replace,$oldURI);
$output=substr($pattern,2,-2);
$output=stripcslashes($output);
foreach($替换为$key=>$value)
$output=str_replace(“(?P[a-zA-Z]+)”,$value$output);

谢谢!但不幸的是,我在整个项目中都使用了正则表达式!我使用regex:D编写了一个路由系统,现在我有了一个linkAnchor类,它传递控制器的名称空间并返回一个修饰的链接。它必须在正则表达式列表中搜索并找到匹配项:(回答问题了吗?因为我在模式中使用了字符串,所以我无法使用数组进行替换。谢谢,不一定可以反转正则表达式;Zend Framework在其路由器中通过使用sprintf格式来指定如何重新组装路由来解决这一问题:(请参阅该部分中的最后两个示例)这很有用;)…谢谢!
$output = substr($pattern, 2, -2);
$output = stripcslashes($output);
foreach ($replacement as $key => $value)
    $output = str_replace("(?P<$key>[a-zA-Z]+)", $value, $output);