使用php正则表达式(查看内部)从宏中修剪到根域的链接


Trim link to root domain from macros with php regex (look inside)

我的php脚本包含宏===shortlink===。此宏位于EOF容器中

<?php
$gtemplate=<<<EOF
 ===shortlink===
EOF;
?>

===短链接===包含这样的urlhttp://site.com/2012/blog-post.html当我运行脚本时,它显示===shortlink===的结果。类似http://site.com/2012/blog-post.html的URL。我需要从这个宏中修剪url以仅显示域名为site.com。建议我如何在这个宏中用php-regex修剪它?

看起来这个宏应该返回到特殊的var,然后修剪,然后返回到===shortlink===。嘿,大师们,你们打败它了吗?

试过这种

$urlpage = '===shortlink===';
preg_match_all("/((?:[a-z][a-z''.''d''-]+)''.(?:[a-z][a-z''-]+))(?![''w''.])/", $urlpage, $replurl, PREG_PATTERN_ORDER);
    $replurl= '===shortlink===';

但它不起作用。请帮我换一个简单的。

为什么不使用parse_url()

<?php
    $url = "http://site.com/2012/blog-post.html";
    $parsed = parse_url($url);
    echo $parsed["host"];

在这里,您可以通过ctrtard.com查看完整的工作代码,特别感谢。

<?php
function trim_url_to_root_domain($target) {
/* Usage:
* echo trim_url_to_root_domain("http://mytarget.com");
* or
* echo ucfirst(trim_url_to_root_domain("http://mytarget.com")); // uppercase first letter
*/
    // trim http, https, and //
    $target = str_replace("http:", "", $target);
    $target = str_replace("https:", "", $target);
    $target = str_replace("//", "", $target);
    // check for dots
    $dots = substr_count($target, '.');
    if ($dots > 1) { // if there are more than 1 dots, we need to remove subdomain
        $first_dot = strpos ($target, ".");
        $target = substr($target, $first_dot+1, strlen($target));
    }
    // find the last slash, this should be the slash just before directory info
    $last_slash = strripos($target, "/");
    if ($last_slash > 0 ) {
        $target = substr($target, 0, $last_slash);
    }
    // find last slash one more time, to handle targets like /domain.com
    $last_slash = strripos($target, "/");
    if ($last_slash !== FALSE ) {
        $target = substr($target, 1, strlen($target));
    }
    // normalize target so it's all lower case
    $target = strtolower($target);
    return $target;
}

echo trim_url_to_root_domain("http://mytarget.com");
echo ucfirst(trim_url_to_root_domain("http://mytarget.com"));
?>