使用str_replace()并返回两个不同的版本

时间:2011-03-17 22:24:02

标签: php str-replace

我有一个textarea,我可以输入多个域名来检查可用性。 在我下面的脚本中,我用“ - ”替换空格。但我也想要一个没有“ - ”的域名版本。

比如说我输入:

一个好词

另一个上帝的话

然后我希望脚本同时返回:

一个良好的字

A-诺特尔神字

还有:

agoodword

anothergodword

$domaininput = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
$change_space = str_replace(" ","-",$domaininput);
$change_new_line = str_replace("\n",",",$change_space);
$manydomains = explode("," , $change_new_line);
foreach ($manydomains as $domain){
//some code
}

如何在PHP中完成?

3 个答案:

答案 0 :(得分:0)

尝试:

$domaininput = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
$change_space = str_replace(" ","-",$domaininput);
$change_space .= str_replace(" ", "", $domaininput);
$change_new_line = str_replace("\n",",",$change_space);
$manydomains = explode("," , $change_new_line);
foreach ($manydomains as $domain){
//some code
}

答案 1 :(得分:0)

$manydomains = ... // as above...

// Get alternatives by removing dashes:
$alternatives = explode(",", str_replace("-", "", $change_new_line));

// If a domain name did not contain a dash, there will be duplicates in
// $manydomains and $alternatives.  array_unique() takes care of those.
$manydomains = array_unique(array_merge($manydomains, $alternatives));

答案 2 :(得分:0)

如果你还没有让它工作,那么这项工作的粗略功能加上它只会返回独特的域名

function getDomains($domaininput){
    function domainA($domaininput){
        $return = str_replace(" ","-",$domaininput);
        return $return;
    }
    function domainB($domaininput){
        $return = str_replace("-","",$domaininput);
        return $return;
    }
    $domaininput = str_replace("\n",",",$domaininput);
    $return = domainA($domaininput)."\n";
    $return .=domainB($return);
    $return = str_replace("\n",",",$return);
    $return = trim($return,'-,');
    $return = explode(",",$return);
    $return = array_unique($return);
    return $return;
}


$manydomains = getDomains($domaininput);