我可以简化这个PHP表达式吗?
我有下面的PHP函数,我想简化,什么是最好和最有效的方法我可以简化这个PHP表达式吗?,php,replace,Php,Replace,我有下面的PHP函数,我想简化,什么是最好和最有效的方法 function XMLToObject($dirtyXML) { $clean_xml = str_ireplace([ 'SOAP-ENV:', 'SOAP:' ], '', $dirtyXML); $clean_xml2 = str_ireplace([ 'common:',
function XMLToObject($dirtyXML) {
$clean_xml = str_ireplace([
'SOAP-ENV:',
'SOAP:'
], '', $dirtyXML);
$clean_xml2 = str_ireplace([
'common:',
''
], '', $clean_xml);
$clean_xml3 = str_ireplace([
'pdt:',
''
], '', $clean_xml2);
$clean_xml4 = str_ireplace([
'apd:',
''
], '', $clean_xml3);
$clean_xml5 = str_ireplace([
'bs7666:',
''
], '', $clean_xml4);
$xml = simplexml_load_string($clean_xml5);
return $xml;
}
我在考虑使用另一个函数,比如
function replace($oldVal,$newVal)
然后用我的不同值对重复4次 您可以将所有要替换的事件合并到一个数组中:
function XMLToObject($in) {
$find = array('SOAP-ENV:', 'SOAP', 'common:', 'pdt:', 'apd:', 'bs7666:');
$replace = '';
return simplexml_load_string(str_ireplace($find, $replace, $in));
}
此外,用空字符串替换空字符串也没有什么价值…将所有替换的值合并到一个数组中这样做?-str_-ireplace(['pdt:',''SOAP-ENV:','SOAP:']准确地-
str_-ireplace(数组(…),'$var)
阅读有关函数的文档