2

I'm trying to figure out how to replace the title portion of an image (title="Title is here") in PHP, but I cannont get it to work, so could someone please help?

The title could be literally anything, so I need to find title"{anything here}" and replace that (as below).

I'm trying to us preg_replace(), but if there is a better way, I'm open to suggestions.

I've tried several different variations, but I think this is not too far off the mark -

$pattern = '#^title="([a-zA-Z0-9])"$#';
$replacement = 'title="Visit the '.$service['title'].' page';
$service_image = preg_replace($pattern, $replacement, $service_image);
000
  • 3,976
  • 4
  • 25
  • 39
David Gard
  • 11,225
  • 36
  • 115
  • 227
  • How about using Jquery for that? – Priyank Patel Aug 21 '12 at 10:03
  • 1
    You're missing a double quote : `'title="Visit the '.$service['title'].' page"'`, and your pattern doesn't accept spaces or dashes – zessx Aug 21 '12 at 10:04
  • @freebird: How about not? OP wants this on the server side, jQuery is not a magic solution. – Madara's Ghost Aug 21 '12 at 10:05
  • 2
    Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost Aug 21 '12 at 10:05
  • 1
    possible duplicate of [How to parse and process HTML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – PeeHaa Aug 21 '12 at 10:21
  • @freebird - No jQuery thanks, I'm looking to do this server side. – David Gard Aug 21 '12 at 11:16
  • @samsamX - Thanks for the heads up, I was missing a quote, but I've gone in another direction with DOMDocument. – David Gard Aug 21 '12 at 11:17

2 Answers2

6
<?php

$html = '<img src="whatever.jpg" title="Anything">';


$dom = new DOMDocument;
$dom->loadHTML($html);
$img = $dom->getElementsByTagName("img")->item(0);
/** @var $img DOMElement  Now, $img contains the DOM note representing the image. */
$img->setAttribute("title", "Whatever you want here!");

/* Export the image alone (if not used like this,
 * you'd get a complete HTML document including head and body).
 *
 * This ensures you only get the image.
 */
echo $dom->saveXML($img);

No regex for HTML please. This will work for you.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Thaks for the heads up, and it does seem better than the RegEx. I'm getting an error though - `Fatal error: Class 'DOMDocument' not found in ...`. Is there a file I have to include, or is this to do with my PHP setup? – David Gard Aug 21 '12 at 10:23
  • @DavidGard: What version of PHP are you running? DOM is enabled by default. – Madara's Ghost Aug 21 '12 at 10:24
  • @Truth - I've managed to make DOMDocument available by installing `php53-xml`, and you code works perfectly. Thanks for the help (and the education in not parsing HTML with RegEx!!). Thanks. – David Gard Aug 21 '12 at 11:15
-2

Use this snippet :

$tag = '<img title="My Old Title" src="localhost"  alt="this is the alt"/>';
echo preg_replace('/(title)=("[^"]*")/i','title="My New Title"',$tag);
// <img title="My New Title" src="localhost" alt="this is the alt">
amd
  • 20,637
  • 6
  • 49
  • 67