views:

269

answers:

2

I am trying to match <a> tags within my content and replace then with the link text followed by the url in square brackets for a print-version. The following example works if there is only the "href". If the <a> contains another attribute, it matches too much and doesn't return the desired result. How can I match the URL and the link text and that's it?

Here is my code:

<?php
$content = '<a href="http://www.website.com"&gt;This is a text link</a>';
$result = preg_replace('/<a href="(http:\/\/[A-Za-z0-9\\.:\/]{1,})">([\\s\\S]*?)<\/a>/',
     '<strong>\\2</strong> [\\1]', $content);
echo $result;
?>

Desired result:

<strong>This is a text link </strong> [http://www.website.com]

Thanks, Jason

A: 

You can make the match ungreedy using ?. You should also take into account there may be attributes before the href attribute.

$result = preg_replace('/<a [^>]*?href="(http:\/\/[A-Za-z0-9\\.:\/]+?)">([\\s\\S]*?)<\/a>/',
    '<strong>\\2</strong> [\\1]', $content);
Greg
+7  A: 

You should be using DOM to parse HTML, not regular expressions...

Edit: Updated code to do simple regex parsing on the href attribute value.

Edit #2: Made the loop regressive so it can handle multiple replacements.

$content = '
<p><a href="http://www.website.com"&gt;This is a text link</a></p>
<a href="http://sitename.com/#foo"&gt;bah&lt;/a&gt;

<a href="#foo">I wont change</a>

';


 $dom = new DOMDocument();
    $dom->loadHTML($content);

    $anchors = $dom->getElementsByTagName('a');
    $len = $anchors->length;

    if ( $len > 0 ) {
        $i = $len-1;
        while ( $i > -1 ) {
        $anchor = $anchors->item( $i );

        if ( $anchor->hasAttribute('href') ) {
            $href = $anchor->getAttribute('href');
            $regex = '/^http/';

            if ( !preg_match ( $regex, $href ) ) { 
         $i--;
         continue;
            }

            $text = $anchor->nodeValue;
            $textNode = $dom->createTextNode( $text );

            $strong = $dom->createElement('strong');
            $strong->appendChild( $textNode );

            $anchor->parentNode->replaceChild( $strong, $anchor );
        }
        $i--;
        }
    }

    echo $dom->saveHTML();
    ?>
meder
bare with me, trying to reformat it :p
meder
finally - and there we go.
meder
Thanks for the effort meder. I need to dive more into DOM manipulation.
JasonBartholme