PHP:替换相对顶部URL/&引用;使用绝对域URL
我想将以PHP:替换相对顶部URL/&引用;使用绝对域URL,php,url,preg-replace,relative-url,Php,Url,Preg Replace,Relative Url,我想将以。/stuff/more.php开头的相对URL转换为http://www.example.com/stuff/more.php在我的RSS源中 我使用的PHP代码如下所示: $content = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#", '$1http://www.example.com/$2$3', $content); $content=preg
。/stuff/more.php
开头的相对URL转换为http://www.example.com/stuff/more.php
在我的RSS源中
我使用的PHP代码如下所示:
$content = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#", '$1http://www.example.com/$2$3', $content);
$content=preg\u replace(“#”(]*href\s*=\s*[\'”)(?!http)([^\'>]+)([\'>]+)([\'>]+)。”$1http://www.example.com/$2$3',$content);
结果是错误的想法,它返回如下URL
http://www.example.com/../stuff/more.php
请注意,。/
部分尚未删除,请帮助
基本上如此。
这就是我所拥有的:。/stuff/more.php
这就是我(运行上述代码后)得到的结果:http://www.example.com/../stuff/more.php
这就是我想要的:
http://www.example.com/stuff/more.php
为什么不用域替换前两个点
$result = str_replace('..', 'http://www.example.com', $contet, 1);
使用
$\u SERVER[HTTP\u HOST]$\u SERVER[REQUEST\u URI]
是PHP中获取绝对url的全局变量。好吧,我将开始研究正则表达式。大部分看起来都很好(事实上,这里有一个足够好的正则表达式,我有点惊讶您在其他方面遇到了麻烦!)但结尾有点奇怪——最好是这样:
#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"']>)#
\n”; 如果(substr($m[2],0,3)='../')) $m[2]=substr($m[2],3); $content=$m[1]。'http://www.example.com/“.$m[2]。$m[3]; } #########从上面抄到这里 echo“B content=$content
\n”; } (我提供了一个小型测试套件,它围绕着您要寻找的内容—您只需要在代码中添加标记行即可。)添加(\.\.\.\.\.\/)*应该可以 $content=preg\u replace(“#”(]href\s=\s*[\”)(?!http)(../../../../../../)*([^\”>]+)([\“'>]+)。”$1http://www.example.com/$3$4',$content) 此外,附注2$3已更改为$3$4 编辑: 减少到一个备选方案:
$content = preg_replace("#(<a href=\"\.\.\/)#", '<a href="http://www.example.com/', $content);
$content=preg\u replace(“#”(]*href\s*=\s*[\”)(?!http)(\.\.\/)*([^\'>]+)([\“'>]+)\”,“$1http://www.example.com/$3$4',$content);
我找到了解决方案,感谢所有在这方面帮助我的人。
以下是我使用的代码:
$content=preg_replace(“#)(第一个代码是检测相对URL所必需的,不是吗?或者有其他情况吗?我想在我当前的代码中添加“./”语法。RSS混合了URL,一些带有http://和一些带有../你能告诉我如何在我现有的代码中添加“./”语法吗?你能比我更具体一些吗你的代码不起作用?我试过了,它没有取代RSS提要上的“./”我可以在私人聊天中为你提供RSS链接和完整的php代码,如果你想帮助将我的精确迷你测试复制到一个新的php文件中,有人能给我提供“./”is([..\”>])的语法吗
correct?实际上它也不起作用,但你的思路是正确的。我希望代码能准确地搜索这个href=“../
并用这个href=”替换它=http://www.example.com/
@user4697489请告诉我写这封信的格式href=“../
我将把它缩减为1种可选:(\.\.\/)*我制作了这个代码$content=preg\u replace(“#”)(您也可以使用explode和inflade来摆脱启动。/
$content = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)(\.\.\/)*([^\"'>]+)([\"'>]+)#", '$1http://www.example.com/$3$4', $content);
$content = preg_replace("#(<a href=\"\.\.\/)#", '<a href="http://www.example.com/', $content);