如何剥离http:// www。从PHP函数只留下.com

时间:2012-08-01 07:15:37

标签: php

我有一个优惠券网站,在我的商店页面上显示商店网址。我想要的只是.com在每个商店的末尾没有在开头显示http://变体

这是我的代码,显示商店网址,我只想显示domain.com而不是http://www.domain.com,也可能显示为http://domain.com

<p class="store-url"><a href="<?php echo $url_out; ?>" target="_blank"><?php echo $stores_url; ?>

由于此功能,它显示如下

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

?>

可以做些什么................

3 个答案:

答案 0 :(得分:0)

快速而肮脏 - 展示可能的功能......

<?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 />');
}
?>

<强>输出:

input: xyzexamp.com; output: xyzexamp.com
input: xyzexamp; output: xyzexamp.com
input: xyz examp; output: xyzexamp.com

答案 1 :(得分:0)

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

  1. 使用http://php.net/manual/en/function.parse-url.php
  2. 打破网址
  3. 使用unset
  4. 删除生成的数组的scheme元素
  5. 使用http://www.php.net/manual/en/function.http-build-url.php
  6. 重新构建它

答案 2 :(得分:0)

这是preg_replace的用途:

<?php
$http_url = 'http://www.somestore.com/some/path/to/a/page.aspx';
$domain = preg_replace('#^https?://(?:www\.)?(.*?)(?:/.*)$#', '$1', $http_url);
print $domain;
?>
此代码将打印出

<?php