2

私のフォーラムの 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」のようにしたい

4

1 に答える 1

3

使用できますstr_replace()。このような:

$new_url = str_replace('index.php', 'test/', $original_url);

preg_replace()も機能しますが、より複雑な (そして強力な)ことに注意してください。この場合、 str_replace() が適しています。

参考までに、str_replace のサブジェクト パラメータは元の文字列です。この例では、「index.php」を含む URL です。あなたの例は次のようになります。

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

echo preg_replace($pattern, $replacement, $subject);
于 2013-01-21T20:19:18.603 に答える