0

I want to replace a image with html code, that must be the output of a php function. whenever a image tag is found in an html document, it should be passed to a php function and replaced with a html strings. my expectation is like below.

index.htm file contains hello <img src="image.jpg" /> sentence continuation.

this image.jpg is passed to php function like convert_pic('image.jpg');

so the output of index.htm is hello <div>....</div> sentence continuation.

where <div>....</div> is the out put of php function convert_pic('image.jpg');

so <img src="image.jpg" /> is to be replace by <?php convert_pic('image.jpg'); how it can be done, or any other possibility to attain this?

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
Sakthitharan
  • 49
  • 1
  • 5
  • can you clarify more what you want instead of the image tag? nothing? – M. Suleiman Sep 30 '11 at 07:38
  • actually I want all my image in a web page is converted to some html content. `` should be `
    some information about the image
    ` the image should not be displayed. instead the div content is displayed in the page
    – Sakthitharan Sep 30 '11 at 07:42
  • Do you want to append and prepend the `
    ` tag to any image found on the page?
    – Damien Pirsy Sep 30 '11 at 07:42
  • can u tell us what the convert_pic method will do to that image? whats the return value? – sicKo Sep 30 '11 at 07:42
  • convert_pic('image.jpg') will convert a jpg image to html content. that content shows the image in color code format. all the pixel in the image is converted into hex triplet (#ffffff) format and then show in a table. – Sakthitharan Sep 30 '11 at 07:45
  • @Sakthi what you say in your last comment varies *vastly* from your original requirement in your question. Which one is it? Anyway, you will probably want to look at a HTML parser. [Best methods to parse HTML with PHP](http://stackoverflow.com/questions/3577641/3577662#3577662) – Pekka Sep 30 '11 at 07:50
  • preg_replace() can be used, but how to pass the entire html to it and once again convert_pic should be interpreted. – Sakthitharan Sep 30 '11 at 07:54
  • You may want to look at a HTML parser instead of regular expressions: [Best methods to parse HTML with PHP] – Pekka Sep 30 '11 at 07:55
  • @Pekka No variation, I tried to make the question clear. Can you specify where is the variation – Sakthitharan Sep 30 '11 at 07:57
  • @Sakthi ah, okay - I thought the converting pixels stuff is part of your question. I didn't realize you already have that, sorry – Pekka Sep 30 '11 at 07:58
  • I also tried Url RewriteRule for any jpg image that pings the server, it wil to redirected to the php file which contains convert_pic() method, but still can't display it in html – Sakthitharan Sep 30 '11 at 08:00

1 Answers1

0

With the DOM extension you can use XPath to find all img tags and then replace them
see also:

e.g

<?php
// closure/lambda, php 5.3+ only
$convert = function($src) {
    return '---'.$src.'---';
};

$doc = new DOMDocument;
$doc->loadhtml(getHTML());
echo "before: ", $doc->savehtml(), "\n\n";
foo($doc, $convert);
echo "after: ", $doc->savehtml(), "\n\n";


function foo(DOMDocument $doc, $fn) {
    $xpath = new DOMXPath($doc);

    $imgs = array();
    foreach( $xpath->query('/html/body//img') as $n ) {
        $imgs[] = $n;
    }

    foreach($imgs as $n) {
        $txt = $fn($n->getAttribute('src'));
        $div = $doc->createElement('div', $txt);
        $n->parentNode->replaceChild($div, $n);
    }
}

function getHTML() {
return '<html><head><title>...</title></head><body>
    <p>lorem ipsum <img src="a.jpg" alt="img#1"/></p>
    <p>dolor sit amet<img src="b.jpg" alt="img#2"/></p>
    <div><div><div><img src="c.jpg" alt="img#3" /></div></div></div>
</body></html>';
}

prints

before: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>...</title></head><body>
    <p>lorem ipsum <img src="a.jpg" alt="img#1"></p>
    <p>dolor sit amet<img src="b.jpg" alt="img#2"></p>
    <div><div><div><img src="c.jpg" alt="img#3"></div></div></div>
</body></html>


after: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>...</title></head><body>
    <p>lorem ipsum <div>---a.jpg---</div></p>
    <p>dolor sit amet<div>---b.jpg---</div></p>
    <div><div><div><div>---c.jpg---</div></div></div></div>
</body></html>
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • So I could add my conversion script here ah? `$convert = function($src) { return '---'.$src.'---'; };` – Sakthitharan Sep 30 '11 at 08:10
  • How can I pass the .html file to this, I don't know how fuction getHTML() works – Sakthitharan Sep 30 '11 at 08:13
  • yes. This function has to return a string that will become the text-node contents of the new div. – VolkerK Sep 30 '11 at 08:16
  • Instead of loadhtml() you can use loadhtmlfile(), see http://docs.php.net/domdocument.loadhtmlfile – VolkerK Sep 30 '11 at 08:17
  • btw: text-node means "html tags are not recognized as such", they will become text. If you need html tags in the replacement it'll be a bit more complex. – VolkerK Sep 30 '11 at 08:19
  • for all the pages in a directory I need to run this. Can I get $file variabel from url and then pass it to this file,so that loadhtmlfile($file) has the current file requested – Sakthitharan Sep 30 '11 at 08:22
  • So the last doubt of mine, if I can't use html tags inside the conversion, then how it can be done – Sakthitharan Sep 30 '11 at 08:28