合并两个函数,str_replace?
Merge two functions, str_replace?
从 Feed 导入数据时,我需要替换一些字符。我不确定如何将两个函数合并为一个以使 url 正确。
试图将一个函数置于另一个函数中,但没有成功。
https://www.xxxxxx.se/brand/[my_custom_function2([my_custom_function3({品牌[1]})])]/
如何将以下两个合二为一?
<?php
function my_custom_function2($string) {
$string = str_replace (" ", "-", $string);
return $string;
}
function my_custom_function3($string) {
$string = str_replace ("Aéryne", "Aeryn", $string);
return $string;
}
?>
谢谢!
PS。我有数百个品牌,有些是两个词。例如 "Billi Bi"。这就是我需要第一个功能的原因。
<?php
function combine_functions($string){
$string = my_custom_function2($string);
$string = my_custom_function3($string);
return $string;
}
function my_custom_function2($string) {
$string = str_replace (" ", "-", $string);
return $string;
}
function my_custom_function3($string) {
$string = str_replace ("Aéryne", "Aeryn", $string);
return $string;
}
?>
应该这样做
您可以通过在 str_replace
中传递数组来使用单个调用替换多个字符
str_replace (array("Aéryne"," "), array("Aeryn","-"), $string);
在单个函数中包装上面你不需要 n str_replace() 调用来替换 n 个字符
为什么不这样调用函数
$result = my_custom_function2(my_custom_function3(品牌[1]));
$url = "https://www.xxxxxx.se/brand/{$result}/";
从 Feed 导入数据时,我需要替换一些字符。我不确定如何将两个函数合并为一个以使 url 正确。
试图将一个函数置于另一个函数中,但没有成功。
https://www.xxxxxx.se/brand/[my_custom_function2([my_custom_function3({品牌[1]})])]/
如何将以下两个合二为一?
<?php
function my_custom_function2($string) {
$string = str_replace (" ", "-", $string);
return $string;
}
function my_custom_function3($string) {
$string = str_replace ("Aéryne", "Aeryn", $string);
return $string;
}
?>
谢谢!
PS。我有数百个品牌,有些是两个词。例如 "Billi Bi"。这就是我需要第一个功能的原因。
<?php
function combine_functions($string){
$string = my_custom_function2($string);
$string = my_custom_function3($string);
return $string;
}
function my_custom_function2($string) {
$string = str_replace (" ", "-", $string);
return $string;
}
function my_custom_function3($string) {
$string = str_replace ("Aéryne", "Aeryn", $string);
return $string;
}
?>
应该这样做
您可以通过在 str_replace
str_replace (array("Aéryne"," "), array("Aeryn","-"), $string);
在单个函数中包装上面你不需要 n str_replace() 调用来替换 n 个字符
为什么不这样调用函数 $result = my_custom_function2(my_custom_function3(品牌[1]));
$url = "https://www.xxxxxx.se/brand/{$result}/";