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

发布于 2024-10-23 00:59:10 字数 635 浏览 6 评论 0原文

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

举例来说,我输入:

一个好词

god-word

然后我希望脚本返回两者:

a-good-word

a-nother-

a-nother-god-word 还有:

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 中做到这一点?

I have a textarea where I can type in multiple domain names to check availability.
In my script below I replace space with “-“. But I also want a version of the domain name without “-”.

Say for example that I type in:

a good word

a nother god word

Then I want the script to return both:

a-good-word

a-nother-god-word

And also:

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
}

How can that be done in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情丝乱 2024-10-30 00:59:10
$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));
$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));
滥情哥ㄟ 2024-10-30 00:59:10

尝试:

$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
}

Try:

$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
}
冷情 2024-10-30 00:59:10

如果你还没有让它工作,这里有一个粗略的函数来完成这项工作,而且它只会返回唯一的域

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);

if you not got it to work yet here a crude function todo the job plus it will only return unique domains

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文