【问题标题】:Extract links from content [duplicate]从内容中提取链接[重复]
【发布时间】:2019-03-06 18:46:09
【问题描述】:

我正在寻找一种解决方案,仅在我的单个页面上显示字符串内的链接(我所见即所得的内容)。

首先提取我所有的链接,并用链接标题属性替换每个链接内容。

这是我的内容的示例:

<p>Iriaequam igit adhuidie eo, condam ciorteli pripsenit Catu quam nos, sediess ilint. Scipios alabi 
    <a title="link title 1" href="http://www.google.com" target="_blank" rel="1 noopener">incepopori</a> 
    senatifec iam pra re hoc, caet? Bus viritid 
    <a title="Link title 2" href="http://www.facebook.com" target="_blank" rel="2 noopener">epectam</a> 
    etorum imus revilla dit fore tem. Quam fugitas sitius coribusciam, voluptam alique velibus ut dit earum simodia quo conseque vit, cusa core pro odictur aut hilitatquat et atur amet et veliquatur. Ici aceruptae es.
</p>

这是我想在我的页面上显示的内容:

<a href="http://www.google.com" target="_blank" rel="1">link title 1</a>
<a href="http://www.facebook.com" target="_blank" rel="2">link title 2</a>

这是我迄今为止尝试过的:

<?php 

$post_content = get_the_content();

preg_match_all('/href="(.*?)"/s', $post_content, $matches);

$count = count($matches[1]);

for ($row = 0; $row < $count ; $row++) {

    echo "<a href=".$matches[1]["$row"]." target='_blank' rel='link rel'>link title</a><br />";

}

?>

这是我得到的:

<a href="http://www.google.com" target="_blank" rel="link rel">link title</a><br>
<a href="http://www.facebook.com" target="_blank" rel="link rel">link title</a>

我的问题是我找不到获取 rel 属性的方法,并无法将链接内容替换为 title 属性。

有什么想法吗?

感谢您的帮助

【问题讨论】:

    标签: php regex hyperlink attributes wysiwyg


    【解决方案1】:

    您可以像获得 href 一样获得 rel 和标题:

    preg_match_all('/href="(.*?)"/s', $post_content, $hrefs); // get the hrefs
    preg_match_all('/title="(.*?)"/s', $post_content, $titles); // get the titles
    preg_match_all('/rel="(.*?)"/s', $post_content, $rels); // get the rels
    preg_match_all('/>([^>]*)<\/a>/s', $post_content, $contents); // get the link contents
    
    $count = count($hrefs[1]);
    
    for ($row = 0; $row < $count ; $row++) {
    
        // Note that I've added the `href` quotes.
        echo "<a href='".$hrefs[1]["$row"]."' target='_blank' rel='".$rels[1]["$row"]."'>".$contents[1]["$row"]."</a><br />";
    
    }
    

    【讨论】:

      【解决方案2】:

      看看这里:https://regexr.com/40f21

      我已经建立了一个正则表达式来捕获一行,例如您的示例:/&lt;a href="(.*)" target="(.*)" rel="(.*)"\&gt;(.*)&lt;\/a&gt;/isU。我添加的附加标志是 U 表示不贪婪,i 表示不区分大小写。

      您可以在底部窗口中看到您的 google 示例返回的匹配数组如下:

      [0] = <a href="http://www.google.com" target="_blank" rel="1">link title 1</a> (the matched string)
      [1] = http://www.google.com (the src)
      [2] = _blank (the target)
      [3] = 1 (the rel)
      [4] = link title 1 (the link text)
      

      请注意,这根本不灵活,如果链接与您在示例中给出的格式不完全匹配,那么它将不匹配。可能更好的方法是让正则表达式匹配&lt;a&gt; - &lt;/a&gt; 的打开和关闭并捕获其间的所有内容。然后在空间上爆炸处理捕获的内容,然后再次在等于上爆炸并计算出你得到了什么。这意味着,例如,如果链接碰巧没有目标属性,那么您仍然可以处理它。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        • 2011-09-15
        • 2016-03-23
        • 1970-01-01
        • 1970-01-01
        • 2011-12-14
        • 1970-01-01
        相关资源
        最近更新 更多