删除网站 http/https,同时添加 URL

Remove website http/https while adding URL

在我的自定义字段中,用户添加他们的站点 URL。我想像这样显示它们 example.com 而不是 http//example.com/。我想删除 http/https。这只是为了展示目的。

<?php

$site = get_user_meta( $current_user->ID, 'url', true );

echo '<a class="user-website" href="'.$site.'">'.$site.'</a>';
?>

显示时,显示整个URL,如http://example.com in a link. I want to remove the protocol and display like example.com

谢谢

<?php

$site = get_user_meta( $current_user->ID, 'url', true );
$site_without_http = trim( str_replace( array( 'http://', 'https://' ), '', $site ), '/' );

echo '<a class="user-website" href="'.$site.'">'.$site_without_http .'</a>';

?>

使用正则表达式:

只需 http:// 或 https://

preg_replace(/\w+:\/\//, "", $string);

还有 www.

preg_replace(/\w+:\/\/w{3}\./, "", $string);