str_replace 会起作用吗?如何起作用?

Would str_replace work, and how?

我正在努力阅读 str_replace,但我什么也做不了。我有这段代码,只要曲目或艺术家姓名中没有“'”,它就可以正常工作(文本文件包含:曲目 - 艺术家),这很常见:) 在输出中用“-”替换它应该可以工作(我希望如此)但是如何?

<?php
    $file = "lyrics.txt";

    if (0 < filesize($file)) {
        $myfile = fopen("lyrics.txt", "r") or die("Splat!");
        echo "<a href='https://www.musixmatch.com/search/";
        echo fread($myfile,filesize("lyrics.txt"));
        echo "'target='_blank'><span title='Search lyrics' class='button'>Musixmatch</span> <a/>";
        fclose($myfile);
    }
?>

str_replace 期望搜索字符串、替换字符串和字符串继续工作,并且 returns 具有替换值的新字符串 (可能一直是你的问题)。

就这么简单:

<?php
$input = "some 'foo' with 'bar'";
$input = str_replace("'", "-", $input);
echo $input;
?>

打印:

some -foo- with -bar-

(PHP Sandbox)