更改 <a> 的 href 属性html 文档中的标签

2022-01-02 00:00:00 replace hyperlink dom php html

我正在使用 file_get_contents 来获取某个文件的内容 - 到目前为止,这是有效的.

I'm using file_get_contents to get a certain file's contents -- so far that is working.

现在我想搜索文件并在显示之前将所有 <a href=" 替换为 <a href="site.php?url=文件.

Now I want to search the file and replace all <a href=" with <a href="site.php?url= before showing the file.

我该怎么做?我知道我应该使用某种 str_replace 甚至 preg_replace.但我不知道如何实际搜索并为我使用 file_get_contents 获取的文件执行此操作.

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.

推荐答案

file_get_contents 返回一个包含文件内容的字符串.

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.

这样的事情,使用 str_replace,可能会做:

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;

但请注意,当该属性是 <a 标签的第一个时,它只会替换 href 属性中的 URL...

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...

如果您正在处理 HTML 文档并想要完整"解决方案,请使用 DOMDocument::loadHTML 并使用 DOM 操作方法可能是另一种(更复杂,但可能更强大)的解决方案.

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 :

  • 下链接也
  • file_get_contents - 也获取图片

编辑:

如果要替换两个字符串,可以将数组传递给str_replace的前两个参数.例如:

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);

这样:

  • '<a href="' 将替换为 '<a href="site.php?url='
  • 和 'Pages' 将被替换为 'TEST'
  • '<a href="' will be replaced by '<a href="site.php?url='
  • and 'Pages' will get replaced by 'TEST'

并且,引用手册:

如果搜索和替换是数组,然后 str_replace() 从每个数组并使用它们进行搜索并替换主题.如果更换具有比 search 少的值,那么一个空字符串用于其余部分替换值.如果搜索是一个数组和替换是一个字符串,然后此替换字符串用于搜索的每一个价值.

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 .

如果你想替换 '<a href="' 的所有实例,那么 str_replace 默认就是这样做的 :-)

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

相关文章