0

関連リンクを必要とする html テンプレートを作成していますが、テンプレート化には php (非常に初心者) を使用する必要があります。インデックス ファイルでは、次の例の../ように、href と src からすべてを削除したいと考えています。

page.php
scr="../img/image.jpg"
href="../contact/contact.php"

index.php
scr="img/image.jpg"
href="contact/contact.php"

すべてを削除する関数を作成するにはどうすればよいですか../

ありがとう

4

2 に答える 2

2

が最初に 1 回だけ存在するようにする場合../は、次のように動作します。

echo str_replace('../', $string);

../パスの途中に削除すべきではない が存在する可能性がある場合は、次を使用しpreg_replace()ます。

$str = '../img/image.jpg';
echo preg_replace('~^(\.\./)(.*)$~', "$2", $str);
于 2013-06-13T10:55:29.490 に答える
0

get_file_contents を使用してテンプレートを読み取り、文字列を置き換えます。

$sContents = file_get_contents('<URL TO FILE>');
$sContents = str_replace('../', '', $sContents);
于 2013-06-13T10:55:21.913 に答える