1

I have a dynamic tag element that I need to replace particular tags in with PHP.

Basically I only need the tag <a> part an replace <img> with text. So for example:

<a target="_blank" 
   title="Visit MarcAira14 on Facebook" 
   href="http://www.facebook.com/MarcAira14"
>
  <img alt="Facebook icon" src="/sites/all/modules/contrib/socialmedia/icons/levelten/glossy/48x48/facebook.png">
</a>

would become:

<a target="_blank" 
   title="Visit MarcAira14 on Facebook"
   href="http://www.facebook.com/MarcAira14"
>Facebook</a>

I also need to grab the last string in the title attribute (title="Visit MarcAira14 on Facebook"), in this case "Facebook".

Anyone with an idea or suggestion would be appreciated.

Tomalak
  • 332,285
  • 67
  • 532
  • 628

2 Answers2

2

This is exactly the thing HTML parsers exist for. One way would be to use PHP's own DOMDocument.

Another, maybe simpler approach would be phpQuery, which is a nice wrapper around DOMDocument. (It seems as it has not been updated in a while, though, last update is from May 2009.)

phpQuery borrows heavily from jQuery, so if you are familiar with that, many known concepts can be used.

For your case, it should go something like this:

$doc = phpQuery::newDocument($yourHtmlOrHtmlFragment);

$links = $doc['a[title!=""]:has(img)'];

foreach( $links as $link )
{
    $title = $link->getAttribute('title');
    $words = explode(' ', $title);
    $last_word = end( $words );
    pq($link)->empty()->text( $last_word );
}

Also see the phpQuery manual.

Other HTML parsers for PHP are discussed here: Robust and Mature HTML Parser for PHP.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

This is the regex version. I have tested it and it works. text.html is your source string.

<?php

$string = file_get_contents('text.html');
$pattern = "/<img alt=([^<])*/m";
$replacement = 'Facebook';
echo preg_replace($pattern, $replacement, $string);

?>

It searches for '<img alt=' and then every character until the first '<' and replaces it with Facebook. The m at the end means search multiple lines.

ComFreek
  • 29,044
  • 18
  • 104
  • 156
dmikester1
  • 1,374
  • 11
  • 55
  • 113