如何在PHP中从一个调用调用两个函数?
function 1() {
// do stuff
}
function 2() {
// do other stuff
}
然后我想从一个调用
中调用这两个函数(calling_functions_1_and_2($string));
我该怎么做呢?
阐述:
这个函数去掉任何URL
的字符串。function cleaner($url) {
$U = explode(' ',$url);
$W =array();
foreach ($U as $k => $u) {
if (stristr($u,'http') || (count(explode('.',$u)) > 1)) {
unset($U[$k]);
return cleaner( implode(' ',$U));
}
}
return implode(' ',$U);
}
这个函数去掉字符串中的任何特殊字符等。
function clean($string) {
return $string = preg_replace('/[^A-Za-z0-9'-'']/', '', $string); // Removes special chars.
}
这些函数执行的字符串在JSON数组中。
调用其中一个函数
clean($searchResult['snippet']['title']); // wanting to remove all special characters from this string but not URL's.
但是在下面这个字符串上,我确实想删除特殊字符和url,那么我如何以最有效和最简单的方式调用这两个函数呢?
cleaner($searchResult['snippet']['description']);
创建一个调用两者的函数是一种很好的简单方法:
function clean_both($string)
{
return clean( cleaner( $string ) );
}
这样,你只需要做下面的操作来清理它:
$clean_variable = clean_both( 'here is some text to be cleaned both ways' );
我将为其中一个函数添加第二个参数,让我们取clean()
function clean($string,$urlRemove = false) {
if ($urlRemove) {
$string = cleaner($string);
}
return $string = preg_replace('/[^A-Za-z0-9'-'']/', '', $string); // Removes special chars.
}
function cleaner($url) {
$U = explode(' ',$url);
$W =array();
foreach ($U as $k => $u) {
if (stristr($u,'http') || (count(explode('.',$u)) > 1)) {
unset($U[$k]);
return cleaner( implode(' ',$U));
}
}
return implode(' ',$U);
}
这样,函数clean()将默认只剥离url(当像clean($string);
一样调用时),但是如果你像
clean($string,true);
您将在字符串上执行两个函数