我在 PHP 代码中有一个替换特定文本标记的问题。

huangapple go评论62阅读模式
英文:

i have a replace issue of certain text tag in php code

问题

我有一段PHP代码,我想要将文本"{name}"替换为文件夹中文件名的位置。例如,在文件夹中有4个文件。

  1. mallik-bazar-team-work.html
  2. kolkata-team-work.html
  3. delhi-team-work.html
  4. agra-team-work.html

现在在我的PHP代码中,它将"{name}"替换为"Mallik Bazar"。但它会将"-"替换为空格。但它还会在其他文本标签中将"-"替换为空格。我只想在"{name}"标签中替换它。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$folderPath = '文件夹位置'; // 用实际的文件夹路径替换

$files = scandir($folderPath);

foreach ($files as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) === 'html') {
        $filePath = $folderPath . '/' . $file;

        // 获取没有扩展名的文件名
        $fileName = pathinfo($file, PATHINFO_FILENAME);

        // 提取"-team-work"之前的第一个单词
        $name = str_replace('-team-work', '', $fileName);

        // 读取文件的内容
        $fileContent = file_get_contents($filePath);

        // 用第一个单词替换"{name}"
        $updatedContent = str_replace('{name}', $name, $fileContent);

        $updatedContent = ucwords(str_replace('-', ' ', $updatedContent));

        // 将更新后的内容写回文件
        if (file_put_contents($filePath, $updatedContent) !== false) {
            echo "成功更新 $file<br>";
        } else {
            echo "无法更新 $file<br>";
        }
    }
}
?>
英文:

I have a php code where i want to replace {name} text to the location of file name under a folder. for example in the folder i have 4 files.

  1. mallik-bazar-team-work.html
  2. kolkata-team-work.html
  3. delhi-team-work.html
  4. agra-team-work.html

now i my php code its replace {name} to Mallik Bazar
But its change - to a space. but its also replace - to space in other text tag also. i want to replace it only on {name} tag.

&lt;?php
error_reporting(E_ALL);
ini_set(&#39;display_errors&#39;, 1);

$folderPath = &#39;folder location&#39;; // Replace with the actual path to your folder

$files = scandir($folderPath);

foreach ($files as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) === &#39;html&#39;) {
        $filePath = $folderPath . &#39;/&#39; . $file;

        // Get the file name without the extension
        $fileName = pathinfo($file, PATHINFO_FILENAME);
        // echo $fileName;

        // Extract the first word before &quot;-&quot;
        $name = str_replace(&#39;-team-work&#39;, &#39;&#39;, $fileName);

        // Read the contents of the file
        $fileContent = file_get_contents($filePath);

        // Replace &quot;{name}&quot; with the first word
        $updatedContent = str_replace(&#39;{name}&#39;, $name, $fileContent);
        
        $updatedContent = ucwords(str_replace(&#39;-&#39;, &#39; &#39;, $updatedContent));

        // Write the updated content back to the file
        if (file_put_contents($filePath, $updatedContent) !== false) {
            echo &quot;Successfully updated $file&lt;br&gt;&quot;;
        } else {
            echo &quot;Failed to update $file&lt;br&gt;&quot;;
        }
    }
}
?&gt;

答案1

得分: 0

Your code is almost correct, but there's one issue. The str_replace('-', ' ', $updatedContent) line is converting all the - into spaces within the entire content of the file. If you want to convert - into space only in the {name} tag, you need to do it before replacing {name} in the content.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$folderPath = 'folder location';

$files = scandir($folderPath);

foreach ($files as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) === 'html') {
        $filePath = $folderPath . '/' . $file;
        $fileName = pathinfo($file, PATHINFO_FILENAME);
        $name = str_replace('-team-work', '', $fileName);
        $name = ucwords(str_replace('-', ' ', $name));
        $fileContent = file_get_contents($filePath);
        $updatedContent = str_replace('{name}', $name, $fileContent);

        if (file_put_contents($filePath, $updatedContent) !== false) {
            echo "Successfully updated $file<br>";
        } else {
            echo "Failed to update $file<br>";
        }
    }
}
?>

In this fixed code, ucwords(str_replace('-', ' ', $name)) converts - into space only within the $name variable and then converts the first character of each word to uppercase. The modified $name is then used to replace {name} in the content.

This way, - is replaced with space only in the {name} tag and not anywhere else in the file content.

英文:

Your code is almost correct, but there's one issue. The str_replace(&#39;-&#39;, &#39; &#39;, $updatedContent) line is converting all the - into spaces within the entire content of the file. If you want to convert - into space only in the {name} tag, you need to do it before replacing {name} in the content.

&lt;?php
error_reporting(E_ALL);
ini_set(&#39;display_errors&#39;, 1);

$folderPath = &#39;folder location&#39;;

$files = scandir($folderPath);

foreach ($files as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) === &#39;html&#39;) {
        $filePath = $folderPath . &#39;/&#39; . $file;
        $fileName = pathinfo($file, PATHINFO_FILENAME);
        $name = str_replace(&#39;-team-work&#39;, &#39;&#39;, $fileName);
        $name = ucwords(str_replace(&#39;-&#39;, &#39; &#39;, $name));
        $fileContent = file_get_contents($filePath);
        $updatedContent = str_replace(&#39;{name}&#39;, $name, $fileContent);

        if (file_put_contents($filePath, $updatedContent) !== false) {
            echo &quot;Successfully updated $file&lt;br&gt;&quot;;
        } else {
            echo &quot;Failed to update $file&lt;br&gt;&quot;;
        }
    }
}
?&gt;

In this fixed code, ucwords(str_replace(&#39;-&#39;, &#39; &#39;, $name)) converts - into space only within the $name variable and then converts the first character of each word to uppercase. The modified $name is then used to replace {name} in the content.

This way, - is replaced with space only in the {name} tag and not anywhere else in the file content.

huangapple
  • 本文由 发表于 2023年6月26日 12:16:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定