首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何剥离http://www.从php函数中只留下.com

如何剥离http://www.从php函数中只留下.com
EN

Stack Overflow用户
提问于 2012-08-01 15:15:37
回答 2查看 119关注 0票数 0

我有一个优惠券网站,显示我的商店网页上的商店网址。我想要的是在每个商店的末尾只有.com,而不是在开始时显示

这是我的代码,它显示了一个商店的url,我只想显示domain.com而不是http://www.domain.com,也可以显示为http://domain.com

代码语言:javascript
复制
<p class="store-url"><a href="<?php echo $url_out; ?>" target="_blank"><?php echo $stores_url; ?>

由于此函数,它的显示如下所示

代码语言:javascript
复制
<div class="store"> 
<?php // grab the store meta data 
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$stores_url = esc_url(get_metadata(APP_TAX_STORE, $term->term_id, 'clpr_store_url',       true));
$dest_url = esc_url(get_metadata(APP_TAX_STORE, $term->term_id, 'clpr_store_aff_url',     true));

// if there's a store aff link, then cloak it. else use store url
if ($dest_url)
$url_out = esc_url(home_url(CLPR_STORE_REDIRECT_BASE_URL . $term->slug));
else
$url_out = $stores_url; 

?>

可做的事.

EN

回答 2

Stack Overflow用户

发布于 2012-08-01 15:24:42

快速和肮脏-演示可能的功能...

代码语言:javascript
复制
<?php
function cleanInputString($inputString) {
    // lower chars
    $inputString = strtolower($inputString);

    // remove whitespaces
    $inputString = str_replace(' ', '', $inputString);

    // check for .com at the end or add otherwise
    if(substr($inputString, -4) == '.com') {
        return $inputString;
    } else {
        return $inputString .'.com';
    }
}

// example
$inputStrings = array(
 'xyzexamp.com',
 'xyzexamp',
 'xyz examp'
);

foreach($inputStrings as $string) {
    echo('input: '. $string .'; output: '. cleanInputString($string) .'<br />');
}
?>

输出:

代码语言:javascript
复制
input: xyzexamp.com; output: xyzexamp.com
input: xyzexamp; output: xyzexamp.com
input: xyz examp; output: xyzexamp.com
票数 0
EN

Stack Overflow用户

发布于 2012-08-03 02:46:24

“正确的方式”可能是使用PHP URL处理:

使用http://php.net/manual/en/function.parse-url.php

  • Remove将URL拆分,使用http://www.php.net/manual/en/function.http-build-url.php

取消设置

  • ,重新构建结果数组的方案元素
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11754297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档