替换单词并保持找到的字符串区分大小写
Replace words and maintain case-sensitivity of the found string
我正在创建一个 php 应用程序来格式化文件。所以我需要在维护案例的同时应用查找替换过程。
例如,我需要将'employees'替换为'vehicles'。
$file_content = "Employees are employees_category MyEmployees kitEMPLOYEESMATCH";
$f = 'employees';
$r = 'vehicles';
echo str_ireplace($f, $r, $file_content);
当前输出:
vehicles are vehicles_category Myvehicles kitvehiclesMATCH
期望的输出:
Vehicles are vehicles_category MyVehicles kitVEHICLESMATCH
您可以通过分别替换每个案例来使用类似的东西:
<?php
$file_content = "Employees are employees_category MyEmployees kitEMPLOYEESMATCH";
$f = 'employees';
$r = 'vehicles';
$res = str_replace($f, $r, $file_content); // replace for lower case
$res = str_replace(strtoupper($f), strtoupper($r), $res); // replace for upper case
$res = str_replace(ucwords($f), ucwords($r), $res); // replace for proper case (capital in first letter of word)
echo $res
?>
虽然 SJ11 的答案因其简洁而吸引人,但它很容易对已经替换的子字符串进行意外替换——尽管 OP 的示例数据不可能。
为确保替换不被替换,您必须只对输入字符串进行一次传递。
对于实用程序,我将包括 preg_quote()
,这样当 $r
值包含在正则表达式中具有特殊含义的字符时模式不会中断。
代码:(Demo) (PHP7.4 Demo)
$file_content = "Employees are employees_category MyEmployees kitEMPLOYEESMATCH";
$f = 'employees';
$r = 'vehicles';
$pattern = '~('
. implode(
')|(',
[
preg_quote($f, '~'),
preg_quote(ucfirst($f), '~'),
preg_quote(strtoupper($f), '~')
]
) . ')~';
$lookup = [
1 => $r,
2 => ucfirst($r),
3 => strtoupper($r)
];
var_export(
preg_replace_callback(
$pattern,
function($m) use ($lookup) {
return $lookup[count($m) - 1];
},
$file_content
)
);
输出:(单引号来自var_export()
)
'Vehicles are vehicles_category MyVehicles kitVEHICLESMATCH'
我正在创建一个 php 应用程序来格式化文件。所以我需要在维护案例的同时应用查找替换过程。
例如,我需要将'employees'替换为'vehicles'。
$file_content = "Employees are employees_category MyEmployees kitEMPLOYEESMATCH";
$f = 'employees';
$r = 'vehicles';
echo str_ireplace($f, $r, $file_content);
当前输出:
vehicles are vehicles_category Myvehicles kitvehiclesMATCH
期望的输出:
Vehicles are vehicles_category MyVehicles kitVEHICLESMATCH
您可以通过分别替换每个案例来使用类似的东西:
<?php
$file_content = "Employees are employees_category MyEmployees kitEMPLOYEESMATCH";
$f = 'employees';
$r = 'vehicles';
$res = str_replace($f, $r, $file_content); // replace for lower case
$res = str_replace(strtoupper($f), strtoupper($r), $res); // replace for upper case
$res = str_replace(ucwords($f), ucwords($r), $res); // replace for proper case (capital in first letter of word)
echo $res
?>
虽然 SJ11 的答案因其简洁而吸引人,但它很容易对已经替换的子字符串进行意外替换——尽管 OP 的示例数据不可能。
为确保替换不被替换,您必须只对输入字符串进行一次传递。
对于实用程序,我将包括 preg_quote()
,这样当 $r
值包含在正则表达式中具有特殊含义的字符时模式不会中断。
代码:(Demo) (PHP7.4 Demo)
$file_content = "Employees are employees_category MyEmployees kitEMPLOYEESMATCH";
$f = 'employees';
$r = 'vehicles';
$pattern = '~('
. implode(
')|(',
[
preg_quote($f, '~'),
preg_quote(ucfirst($f), '~'),
preg_quote(strtoupper($f), '~')
]
) . ')~';
$lookup = [
1 => $r,
2 => ucfirst($r),
3 => strtoupper($r)
];
var_export(
preg_replace_callback(
$pattern,
function($m) use ($lookup) {
return $lookup[count($m) - 1];
},
$file_content
)
);
输出:(单引号来自var_export()
)
'Vehicles are vehicles_category MyVehicles kitVEHICLESMATCH'