PHP str_replace更改链接路径 [英] PHP str_replace to change link paths

查看:97
本文介绍了PHP str_replace更改链接路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 file_get_contents 来获取某些文件内容。到目前为止,这是有效的。但后来我想搜索文件并用替换所有< a href =< a href =site.php?url = 显示文件之前。我怎样才能做到这一点?我知道我应该使用某种 str_replace 甚至 preg_replace 。但我不知道如何实际搜索并为我得到的文件 file_get_contents

解决方案

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



因此,您可以使用您想要的任何字符串操作函数来处理此字符串,就像您谈到的那样。 / p>

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

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

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

echo $ new_content;

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



使用正则表达式可能会帮助你更多;但它可能也不会很完美,我担心...



如果你正在使用HTML文档并想要一个完整的解决方案,那么使用< a href =http://php.net/manual/en/domdocument.loadhtml.php =nofollow noreferrer> DOMDocument :: loadHTML 使用DOM操作方法可能是另一种(更复杂,但可能更强大)的解决方案。





这两个答案给出的答案问题也可以帮助你,取决于你愿意做什么:






看到评论后,

编辑



如果要替换两个字符串,可以通过数组到 str_replace 的两个第一个参数。例如:

  $ 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 '



并且,引用手册:


如果搜索和替换是数组,
则str_replace()从每个数组的
获取一个值,并使用它们来搜索
并替换主题。如果替换
的值少于搜索,则
空字符串将用于剩余的
替换值。如果search是一个
数组而replace是一个字符串,那么
这个替换字符串用于
每个搜索值。


如果你想要替换'< a href ='的所有实例,那么它就是 str_replace 默认情况下: - )


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.

解决方案

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 :


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

这篇关于PHP str_replace更改链接路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆