【问题标题】:Modify link target修改链接目标
【发布时间】:2017-04-05 08:01:39
【问题描述】:

在给定的搜索结果页面上,我需要修改特定结果的链接目标,例如:

  • 每个http://www.name.com/company/companyhttp://www.name.com/company/;
  • 每个http://www.name.com/company/somethinghttp://www.name.com/company/#something;
  • 每个http://www.name.com/business/businesshttp://www.name.com/business/;
  • 每个http://www.name.com/business/somethingelsehttp://www.name.com/business/#somethingelse;
  • 每个http://www.name.com/portfolio/projectnamehttp://www.name.com/portfolio/;

请注意,这是一个多语言网站,并且 URL 也可能以 http://www.name.com/en/company/something-en 的形式出现,我需要将其更改为 http://www.name.com/en/company/#something,等等。

我相信这可以通过 PHP 使用正则表达式来实现,但我不够熟练,无法从头开始编写可靠的东西。

实际例子,更改search-contentdiv内的如下链接(wordpress循环生成的链接):

    <div id="search-content">

        <h3><?php _e( 'Search Results for', 'foundationpress' ); ?> <span>"<?php echo get_search_query(); ?>"</span>:</h3>

        <?php if ( have_posts() ) : ?>

            <?php while ( have_posts() ) : the_post(); ?>
                <div id="post-<?php the_ID(); ?>" <?php post_class('blogpost-entry'); ?>>
                    <header>
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    </header>
                    <div class="entry-content">
                        <?php the_excerpt(); ?>
                    </div>
                </div>
            <?php endwhile; ?>

            <?php else : ?>
                <?php get_template_part( 'template-parts/content', 'none' ); ?>

        <?php endif;?>

    </div>

【问题讨论】:

  • 这当然可以在PHP中完成(或者甚至在页面加载后通过浏览器中的JavaScript,我应该认为),但首先我必须问......为什么这个页面上的链接首先是这样的格式? 那个可以改变吗?最好一开始就避免这个问题,而不是写更多的代码来清理它......
  • 是不是如果/first/second/部分匹配,第二部分需要剥离,如果/first/second/部分不匹配则#需要放在前面/#second 部分?
  • 请为此指定一般规则,否则,正则表达式在性能上与 if-else 语句没有什么不同,有时更差。
  • @Kevin_Kinsey 我正在建立一个 wordpress 网站,这些是各个帖子的链接,它们本身并不构成网站的一部分,它们被“拉入”在由几个个别帖子。
  • @varlogtim 没错。基本上就是这样,除了带有语言代码的 URL,其中可能有 http://www.name.com/en/company/company-en > http://www.name.com/en/company 之类的东西。

标签: php regex url hyperlink


【解决方案1】:

Tbis 是函数:

function processHref($input_url) {
    $regex_one  = '#http://([^/]+)/([^/]+)/([^/]+)#';
    $regex_two  = '#http://([^/]+)/([a-z]{2})/(.+)/([^-]+)#';

    if(preg_match($regex_one, $input_url, $matches)) {
        $domain = $matches[1];
        $first  = $matches[2];
        $second = $matches[3]; 
    }
    elseif(preg_match($regex_two, $input_url, $matches)) {
        $domain = $matches[1];
        $lang   = $matches[2];
        $first  = $matches[3];
        $second = $matches[4];
    }

    $output_url = "http://$domain/";

    if(isset($lang))  $output_url .= "$lang/";
    if(isset($first)) $output_url .= "$first/";
    if($first != $second && $first != "portfolio")
        $output_url .= "#$second";

    return $output_url;
}    
?>

【讨论】:

  • 我相信我们已经非常接近我的需要了。只需修改代码,以便我可以将其包含在我拥有 HTML 结构的 php 文件中,是这样吗?
  • 只有一个细节,在投资组合的情况下,我需要剥离第二部分......
  • 在此处添加了“投资组合”的例外情况。
  • 谢谢!您能否修改您的答案,以便代码可以识别给定div 中的所有链接并相应地修改它们?
  • Diogo,你能用一个你将传递的例子来更新这个问题吗? “给定的 div”是指具有特定 ID 的 div 吗?或者确实有text 里面的div?不过一定要更新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
相关资源
最近更新 更多