Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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/1/visual-studio-2012/2.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
使用preg_replace将index.php更改为test_Php - Fatal编程技术网

使用preg_replace将index.php更改为test

使用preg_replace将index.php更改为test,php,Php,在我的论坛URL中,到处都有“index.php”。我想用“测试”来代替它。在我的PHP文件中,我有一行: // Makes it easier to refer to things this way. $scripturl = $boardurl . '/index.php'; 我试着把它改成: // Makes it easier to refer to things this way. $scripturl = $boardurl . '/test'; 然而,这返回了4

在我的论坛URL中,到处都有“index.php”。我想用“测试”来代替它。在我的PHP文件中,我有一行:

// Makes it easier to refer to things this way.
    $scripturl = $boardurl . '/index.php';
我试着把它改成:

// Makes it easier to refer to things this way.
    $scripturl = $boardurl . '/test';
然而,这返回了404个错误。我被告知我需要使用preg_replace来实现这一点。我看了PHP手册,它说我需要一个模式、替换和主题。我对主题部分感到困惑

我试过这个,但没有成功:

// Makes it easier to refer to things this way.
    $scripturl = $boardurl . preg_replace('/index.php','/test','?');
下面是一个URL示例:“domain.com/index.php?node=forum”

我希望它看起来像:“domain.com/test?node=forum”

您可以使用。像这样:

$new_url = str_replace('index.php', 'test/', $original_url);
请注意,这也可以完成这项工作,但它更复杂(也更强大)。str_replace()适合这种情况

仅就您的信息而言,str_replace的subject参数是原始字符串,在您的示例中是包含“index.php”的url。您的示例如下所示:

$pattern = '/index\.php/';
$replacement = 'test/';
$subject = 'http://yoursite.com/index.php?foo=bar';

echo preg_replace($pattern, $replacement, $subject);

您是否尝试过使用
/test/
而不是
/test
?这可能是你的问题。是的,但仍然是同样的问题。stru_替换不是更容易达到你的目的吗?另外,您是否尝试过回显您创建的字符串,以确保它们的外观符合您的预期?我猜如果您的心脏设置为preg_replace,您仍然可以使用它,但它需要第三个参数(您希望通过搜索来进行替换)。在您的例子中:preg_replace('/index.php'、'/test'、'ORIGINAL URL')。我不确定OP是否真的希望php代码执行查找替换。(也许)他想对PHP代码本身进行查找替换。如果是这样,他应该发表评论。我对如何实现我的目标感到困惑。谢谢你的例子,现在对我来说很有意义。虽然,我不认为我可以在我的情况下使用这个。另一个人告诉我,我只是将“$scripturl=$boardurl.'/index.php';”更改为“$scripturl=$boardurl.'/test';”,然后在我的htaccess中放入一些内容以正确执行任何操作。