英文:
i have a replace issue of certain text tag in php code
问题
我有一段PHP代码,我想要将文本"{name}"替换为文件夹中文件名的位置。例如,在文件夹中有4个文件。
- mallik-bazar-team-work.html
- kolkata-team-work.html
- delhi-team-work.html
- 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.
- mallik-bazar-team-work.html
- kolkata-team-work.html
- delhi-team-work.html
- 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.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$folderPath = 'folder location'; // Replace with the actual path to your folder
$files = scandir($folderPath);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'html') {
$filePath = $folderPath . '/' . $file;
// Get the file name without the extension
$fileName = pathinfo($file, PATHINFO_FILENAME);
// echo $fileName;
// Extract the first word before "-"
$name = str_replace('-team-work', '', $fileName);
// Read the contents of the file
$fileContent = file_get_contents($filePath);
// Replace "{name}" with the first word
$updatedContent = str_replace('{name}', $name, $fileContent);
$updatedContent = ucwords(str_replace('-', ' ', $updatedContent));
// Write the updated content back to the file
if (file_put_contents($filePath, $updatedContent) !== false) {
echo "Successfully updated $file<br>";
} else {
echo "Failed to update $file<br>";
}
}
}
?>
答案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('-', ' ', $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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论