我从不同的 xml 文件导入。
结构示例:
1) 1-样本.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<kyero>
<feed_version>3</feed_version>
</kyero>
<property>
<type><![CDATA[Detached Villa]]></type>
<town>Ciudad Quesada</town>
</property>
<property>
<type><![CDATA[Semi Detached Villa]]></type>
<town>Benijofar</town>
</property>
</root>
2-样本.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<kyero>
<feed_version>3</feed_version>
</kyero>
<property>
<type>semi-detached-villa</type>
<town>Villena</town>
</property>
<property>
<type>Semi</type>
<town>Elda</town>
</property>
</root>
3-样本.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<kyero>
<feed_version>3</feed_version>
</kyero>
<property>
<type>Semi-Detached House</type>
<town>Villena</town>
</property>
<property>
<type>Semi - Detached Villa</type>
<town>Elda</town>
</property>
</root>
我需要将节点中的文本值更改为 SemiDetached。 我正在使用 str_replace 函数。
function house_type_propertynew($type){
$type = str_replace('Semi Detached Villa', 'SemiDetached', $type);
$type = str_replace('Detached Villa', 'SemiDetached', $type);
$type = str_replace('Semi-Detached House', 'SemiDetached', $type);
$type = str_replace('Semi', 'SemiDetached', $type);
$type = str_replace('Semi-Detached House', 'SemiDetached', $type);
$type = str_replace('Semi - Detached Villa', 'SemiDetached', $type);
return $type;}
结果是几个值而不是“SemiDetached”:
半独立式 - 半独立式 半独立式独立式 半独立式
也就是说,文本被重复、混合、折叠不同,但没有给出所需的“半分离”结果!
我想澄清一下,在xml文件中,节点中有其他值需要更改为其他文本值,也有节点中的值不需要更改。
需要更改的示例:
function house_type_propertynew($type){
$type = str_replace('Parking Space', 'garage', $type);
$type = str_replace('Apartamento', 'Apartment', $type);
$type = str_replace('Ground Floor', 'Apartment', $type);
$type = str_replace('Flat', 'Apartment', $type);
$type = str_replace('detached house', 'SemiDetached', $type);
$type = str_replace('Semi', 'SemiDetached', $type);
$type = str_replace('Detached Villa', 'SemiDetached', $type);
$type = str_replace('Semi-detached', 'SemiDetached', $type);
$type = str_replace('Semi-Detached House', 'SemiDetached', $type);
$type = str_replace('semi-detached-villa', 'SemiDetached', $type);
$type = str_replace('Semi - Detached Villa', 'SemiDetached', $type);
$type = str_replace('Terraced house', 'Town House', $type);
$type = str_replace('Townhouse', 'Town House', $type);
$type = str_replace('terraced house', 'Town House', $type);
$type = str_replace('House – Townhouse', 'Town House', $type);
$type = str_replace('Quad Bungalow', 'Quad', $type);
$type = str_replace('Quad House', 'Quad', $type);
$type = str_replace('Quadplex', 'Quad', $type);
$type = str_replace('Quad Villa', 'Quad', $type);
$type = str_replace('Chalet', 'Villa', $type);
$type = str_replace('Chalets Pareados', 'Villa', $type);
$type = str_replace('Village house', 'Villa', $type);
$type = str_replace('Village House', 'Villa', $type);
$type = str_replace('Villa with annex', 'Villa', $type);
$type = str_replace('Villas Pareados', 'Villa', $type);
$type = str_replace('Finca / Country Property', 'Country Estate', $type);
$type = str_replace('finca', 'Country Estate', $type);
$type = str_replace('Country Property/Finca', 'Country Estate', $type);
$type = str_replace('Country Property', 'Country Estate', $type);
$type = str_replace('Country House', 'Country Estate', $type);
$type = str_replace('Plot of Land', 'Plot', $type);
$type = str_replace('Building Plot', 'Plot', $type);
$type = str_replace('land', 'Plot', $type);
$type = str_replace('Land', 'Plot', $type);
$type = str_replace('Restoration Project', 'Plot', $type);
$type = str_replace('Duplex apartment', 'Duplex', $type);
$type = str_replace('Bar', 'Restaurants and Bars', $type);
$type = str_replace('Bar/Restaurant', 'Restaurants and Bars', $type);
$type = str_replace('Commercial Unit', 'commercial', $type);
$type = str_replace('Commercial Building', 'commercial', $type);
$type = str_replace('Locales Comerciales', 'commercial', $type);
$type = str_replace('Business premises', 'commercial', $type);
$type = str_replace('Bungalows Planta Baja', 'Bungalow', $type);
$type = str_replace('Bungalows Planta Alta', 'Bungalow', $type);
return $type;}
我做错了什么?
所以它用 SemiDetached 替换所有值,它不起作用!
function house_type_propertynew($type){
$type = preg_replace('/<type>(.*?)<\/type>/', '<type>SemiDetached</type>', $type);
return $type;}
您编写
str_replace
命令的方式,它们是累积的。因此,如果您从“半独立别墅”开始,第一个 str_replace
就会转变为“半独立别墅”。
但是,几行之后,我们搜索“Semi”并将其替换为“SemiDetached”,因此我们的字符串现在是“SemiDetachedDetached”...等等。
为了避免这种情况,您可以将所有可能的搜索作为数组放入
str_replace
的第一个参数中,并避免搜索替换字符串中的内容(即不搜索“Semi”):
function house_type_propertynew($type)
{
return str_replace([
'Semi Detached Villa',
'Detached Villa',
'Semi-Detached House',
'Semi - Detached Villa',
], 'SemiDetached', $type);
}
或者,您可以使用
preg_replace
进行更复杂的模式匹配