0

我有一个优惠券网站,在我的商店页面上显示商店网址。我想要的只是每个商店末尾的 .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; 

?>

可以做什么......

4

3 回答 3

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;
?>
此代码将打印出来

somestore.com

于 2012-08-02T19:03:50.293 回答
0

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

  1. 使用http://php.net/manual/en/function.parse-url.php分解 URL
  2. 使用 unset 删除结果数组的方案元素
  3. 使用http://www.php.net/manual/en/function.http-build-url.php再次构建它
于 2012-08-02T18:46:24.313 回答
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
于 2012-08-01T07:24:42.327 回答