0

I have inserted external content to my page using simple_html_dom.php but all href not working for example :

 href='oldpage.php'

and I want this like to be changed to

href='http://localhost/oldpage.php'

or

href='http://localhost/mynewpage.php'

is there a way to change all href to new one like add some string before it ? Thank you

Maadh
  • 643
  • 4
  • 24
  • so, why not take care of it during the same time you executed the other script? sounds to me like it would be simpler. – Funk Forty Niner Dec 22 '15 at 02:32
  • In case of the first: http://stackoverflow.com/questions/2543196/jquery-to-change-all-the-links-in-the-page – Sam Segers Dec 22 '15 at 02:33
  • @Fred-ii- the script can bring all the html text . I want to update all href to new link like add additional text in-front of it ! – Maadh Dec 22 '15 at 02:35
  • All links are relative? – chris85 Dec 22 '15 at 02:35
  • did you have a look at the link that Sam left you above? (looks like a duplicate) since you did tag as javascript/jquery. – Funk Forty Niner Dec 22 '15 at 02:35
  • @Fred-ii- Yes , but I did not understand it . I need something similar to @ rahul answer but in PHP . like one command or loop to change all href . Thank you – Maadh Dec 22 '15 at 02:40
  • 1
    so why did you add the javascript/jquery tags? you need to remove those if you're wanting a PHP solution, since that implies that you're looking for those methods. You'll need to edit your question in order to let visitors to the question know of what you want/need here. – Funk Forty Niner Dec 22 '15 at 02:42
  • @Fred-ii- can I use this javascript code inside the PHP and then use the result in PHP ? – Maadh Dec 22 '15 at 02:44
  • see this Q&A then for PHP http://stackoverflow.com/q/6240414/ could be something in there you can use. – Funk Forty Niner Dec 22 '15 at 02:44
  • 2
    Simple solution that could cause issues, `str_replace('href="', 'href="http://www.example.com/', $string);` or use domdocument and pull all href attributes. – chris85 Dec 22 '15 at 02:44
  • @chris85 ^^^ Let's only hope somebody doesn't come along and sweep the rug from under your feet ;-) – Funk Forty Niner Dec 22 '15 at 02:46
  • @MuadhProgrammer any issues with any of your answers? – chris85 Dec 23 '15 at 00:45
  • @chris85 Thank you very much , your answer solved the problem. But I don't know why my question get vote down ! – Maadh Dec 25 '15 at 20:04

5 Answers5

1

You can use .each() to iterate your anchor tags. Then using .attr you should be able to get the href attribute value.

Then a string append using + should do the work. So something like the below one should work

$("a.links").each(function(){
    var $this = $(this);
    var currentHref = $this.attr("href");
    var modifiedHref = "your string" + currentHref;

    $this.attr("href", modifiedHref);
});
rahul
  • 184,426
  • 49
  • 232
  • 263
1

You could do a simple string replace like:

str_replace('href="', 'href="http://www.example.com/', $string);

or with domdocument:

$page = '<html><head></head><body><a href="simple"></a><h1>Hi</h1><a href="simple2"></a></body></html>';
$doc = new DOMDocument();
$doc->loadHTML($page);
$as = $doc->getElementsByTagName('a');
foreach($as as $a){
    $a->setAttribute('href', 'http://www.example.com/' . $a->getAttribute('href'));
}
print_r($doc->saveHTML());

output:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head></head><body><a href="http://www.example.com/simple"></a><h1>Hi</h1><a href="http://www.example.com/simple2"></a></body></html>

This doesn't take into account absolute paths, you'll need a regex approach for that..

If the quote types vary you also will need to use a regex for the str_replace example. Can do something like('|") for that then use $1 to match the quote type.

chris85
  • 23,846
  • 7
  • 34
  • 51
1

You only need to add

<base href="http://localhost/"></base>

Inside head section. its create base url for all other anchor tag. will converted Url as you want.

It will work for you. Try it.

Nitesh Pawar
  • 435
  • 2
  • 11
0

here is a pure javascript approach using replace

//replace selector 'a' with your css selector if needed
for(var i=0;i<document.querySelectorAll('a').length;++i){
var new_href=document.querySelectorAll('a')[i].getAttribute('href').replace(/(http:[/]{2}localhost)[/].*/ig, '$1/mynewpage.php')
document.querySelectorAll('a')[i].setAttribute('href',new_href)
}
//
repzero
  • 8,254
  • 2
  • 18
  • 40
0

Just append "/" before your url like

href='oldpage.php' => href='/oldpage.php'
Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28