0

I have an HTML file that has external assets on a secure server that needs a url generated in order to load. I can load the HTML file fine, but none of the images will load. Relative paths wont work. I need to pass the image src attribute to my generateURL method and replace the current relative src paths with my generated url. An example HTML file I need to replace the tags in would be:

<img id="Im0" src="slide1page1/img/1/Im0.png" alt="Im0" width="720" height="540" style="display: none" />
<img id="Im1" src="slide1page1/img/1/Im1.png" alt="Im1" width="136" height="60" style="display: none" />
<img id="Im2" src="slide1page1/img/1/Im2.png" alt="Im2" width="669" height="45" style="display: none" />

My php script that I load the page through is:

<?php
function generateURL($target,$seconds)
{
    $secret = "mysecretpasscode";
    $end    = time() + $seconds;
    $url    = $target . "?e=" . $end;
    $toHash = $secret . $url;
    $secure = $url . "&h=" . md5($toHash);
    return $secure;
}
$id     = $_POST['id'];
$loc    = $_POST['loc'];
$url    = $loc . "slide" . $id . ".html";
$secure = generateURL($url,600); 
$page   = file_get_contents($secure);
?>

if I echo $page, I can see the page as if I just loaded an html page (but no images). What I need to do before that is find all the src tags in the HTML and run them through my generateURL method so I can generate the correct URL with proper hashing to load. I am thinking a str_replace will work, but I am not familiar enough with regex to continue. Any help would be appreciated.

6
  • 5
    Why don't you use PHP DOM instead? Then you can just select the images using xpath. php.net/manual/en/domxpath.query.php Commented Nov 18, 2011 at 19:23
  • I am not familiar with this. I am seeing a lot of XML references. This looks like it may be doing something that I would want: php.net/manual/en/domxpath.query.php#73557 . I will try and find more examples, but if you have a simple one of what I am wanting to do that'd be great Commented Nov 18, 2011 at 19:44
  • ok I am making progress! I am at this point where I have selected all the tags and generated new URLs...How do I replace them now? pastie.org/2885066 Commented Nov 18, 2011 at 20:03
  • ok I am doing $dom->replaceChild($new,$old) in the loop and then after the loop I do echo $dom->saveHTML(); and I get a blank screen. $old is $old = $image->getAttribute('src');, $new is $new = generateURL($loc . $old, 600);...not sure why I am getting a blank screen Commented Nov 18, 2011 at 20:27
  • 1
    Check this thread out: stackoverflow.com/questions/3194875/… Commented Nov 18, 2011 at 20:35

2 Answers 2

0

First have a look at this answer: RegEx match open tags except XHTML self-contained tags

Though I think you could use PHP's preg_replace_callback as follows:

src_regex = "/src=\"([^\"]+\/";
preg_replace_callback(src_regex, generateURL, html);

Do take into account that the second parameter (generateURL) must be a function with only a single argument, take a look at this page.

Sign up to request clarification or add additional context in comments.

Comments

0

A html parser would be better. Provided you've selected all the tags, you can extract the relative path with the function below.

$u = '<img id="Im0" src="slide1page1/img/1/Im0.png" alt="Im0" width="720" height="540" style="display: none" />';
$v = explode(" ", $u);
$x = explode("=", $v[2]);
echo str_replace("\"", "", $x[1]);

Outputs

slide1page1/img/1/Im0.png

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.