0

PHPの特定のポイントでファイルにチェーンを挿入したい。

ここに例があります(私が試したこと):

$insert = "Example of Chain";
$fileContent = file_get_contents($file);
//Example of content: codecodecode <tag> EXAMPLE EXAMPLE </tag> code code code
//Result i need  = codecodecode Example of Chain codecodecode

ご覧のとおり、タグのコンテンツとその nodeValue を削除し、$insert. また、file_get_contents を使用する必要があることに注意してください。

アドバイスと裏技ありがとう

ここで試してみます。このコードは、ファイル コンテンツ内のタグを (file_get_contents によって) 検出し、コードを関数からのテキスト リターンに置き換えretrieveLinksますが、ファイル内のテキストを正しい位置に戻す方法がわかりません。

// '' の間にある場合は表示されないので、そのように自発的に配置します.. 間違いではありません

$start = 'タグ'; $end = '/タグ';

 $startChain = strpos($fileContent, $start);
 $endChain = strpos($fileContent, $end) + strlen($end);

 $text = substr($fileContent, $startChain, $endChain - $startChain);

 require_once("showISComment.php");
 $newText = retrieveLinks($project);

 $content = str_replace($text, $newText, file_get_contents($file));


 //file_put_contents($file, $content);
4

1 に答える 1

0

タグの内容がわかっている場合は、これで十分です

file_put_contents(
    $file,
    str_replace(
        '<tag> EXAMPLE EXAMPLE </tag>',
        $insert,
        file_get_contents($file)
    )
);
于 2013-02-18T13:36:47.343 に答える