php preg-替换 url。
我在 d:/www/web12/index.php
中有一个index.php 其中有一些锚标记,例如
<a href="/docs/corn">
<a href="/docs/mais">
我想要 preg_replace 它们到
<a href="http://localhost/web12/directory/list=corn">
<a href="http://localhost/web12/directory/list=mais">
$url = preg_replace("/docs/", "'.$HTTP_SERVER_VARS['HTTP_HOST'].'/directory/list=", $url);// Can not use `$HTTP_SERVER_VARS['HTTP_HOST']` in `preg_replace`
I have an index.php in d:/www/web12/index.php
there are some anchor tags in it, e.g.
<a href="/docs/corn">
<a href="/docs/mais">
I want preg_replace them into
<a href="http://localhost/web12/directory/list=corn">
<a href="http://localhost/web12/directory/list=mais">
$url = preg_replace("/docs/", "'.$HTTP_SERVER_VARS['HTTP_HOST'].'/directory/list=", $url);// Can not use `$HTTP_SERVER_VARS['HTTP_HOST']` in `preg_replace`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,
$HTTP_SERVER_VARS
已被替换 - 您应该使用$_SERVER
代替。 ($HTTP_SERVER_VARS
仍然有效,但可能会在未来版本的 PHP 中删除)。其次,您发布的代码中的引号看起来有点奇怪 - 您有一个如下所示的字符串:
您的代码所做的是将整个内容嵌入双引号中,这意味着您认为自己正在做的事情连接 - 即点 - 这些实际上是字符串的一部分。
这本身不会导致语法错误。它会破坏你的字符串替换,但代码会运行。
之所以没有,并且您从中收到错误,是因为
$HTTP_SERVER_VARS['HTTP_HOST']
是一个数组变量,并且数组变量不能在不包装的情况下嵌入到字符串中它们位于{}
大括号中。因此,对于当前的代码,即使您将其替换为$_SERVER['HTTP_HOST']
或任何其他数组变量,您仍然会收到相同的错误。为了解决这两个问题,我们需要纠正构建字符串的方式。有两种解决方案,一种使用字符串连接,另一种使用
{}
大括号将变量嵌入到字符串中。我将选择前一个选项,因为它接近您的代码想要执行的操作。首先,删除除
'HTTP_HOST'
周围的所有引号(单引号和双引号)。还要删除开头的点。现在,仅在字符串的固定部分加上引号(双引号或单引号) - 即
'/directory/list='
。最后,将
$HTTP_SERVER_VARS
更改为$_SERVER
。您最终应该得到以下结果:
这应该可行。希望有帮助。
最后一点,替代选项最终的代码如下所示:
...使用
{}
将数组变量嵌入到字符串中。但我建议坚持使用其他方法,除非您对此工作方式感到满意。另外,我建议使用
str_replace
而不是preg_replace
来实现此目的,因为您没有执行任何实际需要正则表达式的操作。另外,您的正则表达式由于斜线标记而被破坏,这对于str_replace
来说不是问题。Firstly,
$HTTP_SERVER_VARS
has been replaced - you should use$_SERVER
instead. ($HTTP_SERVER_VARS
still works, but may be removed in future version of PHP).Secondly, your quote-marks seem a bit odd in the code you're posted - you have a string that looks like this:
What your code does is embed the whole thing in double-quotes, meaning that where you think you're doing concatenation - ie the dots - these are actually part of your string.
This in itself wouldn't result in a syntax error. It would mangle your string-replacement, but the code would run.
The reason it doesn't, and you're getting an error from it, is because
$HTTP_SERVER_VARS['HTTP_HOST']
is an array variable, and array variables can't be embedded in strings without wrapping them in{}
curly braces. Therefore, with your current code, you would still get the same error even if you did replace it with$_SERVER['HTTP_HOST']
, or indeed any other array variable.To correct both problems, we need to correct the way you've built up your string. There are two solutions, one using string concatenation, the other using
{}
curly braces to embed the variable in the string. I'll go with the former option, since it is closes to what your code is trying to do.Firstly, remove all the quote marks (both single quotes and double quotes) except the ones around
'HTTP_HOST'
. Also remove the dot at the begining.Now, put quotes (either double or single) around just the fixed part of the string - ie
'/directory/list='
.Finally, change
$HTTP_SERVER_VARS
to$_SERVER
.You should end up with the following:
This should work. Hope that helps.
As a final note, the alternative option would have ended up with the code looking like this:
...which embeds the array variable inside the string using
{}
. But I'd suggest sticking with the other method unless you're comfortable with the way this works.Also by the way, I'd suggest using
str_replace
rather thanpreg_replace
for this, since you're not doing anything that actually requires a regex. Plus your regex is broken because of the slash marks, which wouldn't be an issue withstr_replace
.Preg 使用 /'s 作为标识符
Preg uses /'s as a identifyer
单引号和双引号的奇怪组合。试试这个:
You have a strange mix of single and double quotes. Try this instead: