0

I'm looking for a solution to only display the links inside a string (my wysiwyg content) on my single page.

First of all extract all my links, and replace each links content by the links title attribute.

here is an example of what my content looks like :

<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>

and here is what I want to diplay on my page :

<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>

Here is what I've tried so far :

<?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 />";

}

?>

and here is what I get :

<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>

my problem is that I can't find a way to get the rel attribute, and to replace the link content by the title attribute.

any thoughts ?

thanks for your help

mmdwc
  • 1,095
  • 6
  • 27
  • 53

2 Answers2

0

You could get the rels and titles just like how you get the hrefs :

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 />";

}
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
0

Have a look here: https://regexr.com/40f21

I have built up a regular expression to catch a single line like in your example: /<a href="(.*)" target="(.*)" rel="(.*)"\>(.*)<\/a>/isU. The additional flags I added were U for ungreedy and i for case insensitive.

You can see in the bottom windows that the returned match arrays for your google example will be as follows:

[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)

Note that this is not at all flexible and if the link does not exactly match the format you have given in your examples then it won't match. Possibly a better approach would be to get the regex to match the open and close of the <a> - </a> and capture everything in between. Then process the captured content with explodes on spaces, then again explodes on equals and work out what you have got. This means if a link doesn't happen to have a target attribute for example then you are still able to process it.

Hope this helps.

SGD
  • 63
  • 1
  • 7