PHP str_replace to change link paths

淺唱寂寞╮ 提交于 2019-12-23 04:35:37

问题


I'm using file_get_contents to get a certain files contents. So far that is working. But then I would want to search the file and replace all <a href=" with <a href="site.php?url= before showing the file. How can I do this? I know I should use some kind of str_replace or even preg_replace. But I don't know how to actually search and do it for the file I'm getting with file_get_contents.


回答1:


file_get_contents returns a string containing the file's content.

So, you can work in this string using whichever string manipulation function you'd want, like the ones you talked about.

Something like this, using str_replace, would probably do :

$content = file_get_contents('http://www.google.com');

$new_content = str_replace('<a href="', '<a href="site.php?url=', $content);

echo $new_content;

But note it will only replace the URL in the href attribute when that attribute is the first one of the <a tag...

Using a regex might help you a bit more ; but it probably won't be perfect either, I'm afraid...

If you are working with an HTML document and want a "full" solution, using DOMDocument::loadHTML and working with DOM manipulation methods might be another (a bit more complex, but probably more powerful) solution.


The answers given to those two questions might also be able to help you, depending on what you are willing to do :

  • underlinks also
  • file_get_contents - also get the pictures

EDIT after seeing the comment :

If you want to replace two strings, you can pass arrays to the two first parameters of str_replace. For instance :

$new_content = str_replace(
    array('<a href="', 'Pages'), 
    array('<a href="site.php?url=', 'TEST'), 
    $content);

With that :

  • '<a href="' will be replaced by '<a href="site.php?url='
  • and 'Pages' will get replaced by 'TEST'

And, quoting the manual :

If search and replace are arrays, then str_replace() takes a value from each array and uses them to do search and replace on subject . If replace has fewer values than search , then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search .

If you want to replace all instances of '<a href="', well, it's what str_replace does by default :-)




回答2:


$text = file_get_contents('some_file');
$text = str_replace('<a href="', '<a href="site.php?url=', $text);



回答3:


$new_content = preg_replace('!(<a\s*[^>]*)href="([^"]+)"!','\1 href="site.php?url=\2"', $content);

I think this should do the trick:

  • it replaces the href of a link, no matter where it is located
  • e.g. works on <a href=".." , <a style="" href="..."



回答4:


If you want to use the remote document on your website but keep the links of that document intact, better use the BASE element to declare the base URI:

<base href="http://example.com/path/to/remote/document">



回答5:


Like the code sent by bisko but, no matter about the enclose ', " or nothing in href

$text = '<a href="http://www.europanet.com.br">Europanet</a>     <a target="_blank" href=\'http://www.webjump.com.br\'>Webjump</a>
<a id="link" href=http://www.euforia.com.br target="_top">Euforia</a>';
$text = preg_replace('|(<a\s*[^>]*href=[\'"]?)|','\1site.php?url=', $text);


来源:https://stackoverflow.com/questions/1251859/php-str-replace-to-change-link-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!