I am using cURL to get a few web pages. To solve the problem of relative paths, I set the base tag href attribute to be the page cURL downloads.
But I want all the clickable links on the page returned by cURL to have this “http://example.com?url=” before the actual link. So I want all links to be proceeded by the above url.
Well actually base would not change the actual href attribute, but would rather change the link upon clicking. But how can I make it so base will change to what I want upon clicking (proceeded by “http://example.com?url=”) , in addition to the absolute mapping.
Can you elaborate on how I would do this? Let’s say I wanted to change every href attribute of every <a> tag on the page to have all the links be proceeded by a string of my choice (e.g. turn all href=“index.php” to href=“mystring?index.php”). What would the preg_replace look like?
<?php
// set up our test string
$string = '
<html>
<body>
Some html here
<a href="file1.php">link</a>
some more html here and then
<a HREF="file2.php">second link</a>
</body>
</html>.';
// specify our find and replace strings /i is to make find case insensitive
$find = '/href="/i';
$replace = 'href="http://mystring?';
// parse the string
$string = preg_replace($find, $replace, $string);
// echo results to browser
echo htmlspecialchars($string);
?>
Yes but see I’m trying to ONLY do it for <a> tags. This would change all tags with a set href attribute.
And you can’t say $find = '<a href=" ’ because not all sites have href as the first attribute in all their links. So it wouldn’t work for <a title=“blah” href=“blah”>blah</a>
Thanks for your help Monkey. Would you mind explaining what exactly the replace functions look for and replace / what they do? They are my weakness as you can see. Thank you!!
if the link starts with http, then we assume this is an absolute link so just use the full path. If it does not, we assume it’s a relative path and add the base url of the site to the begining of the link, transforming it into an absolute path