开发者

php preg-replace url.

开发者 https://www.devze.com 2023-02-08 16:28 出处:网络
I have an index.php in d:/www/web12/index.php there are some anchor tags in it, e.g. <a href=\"/docs/corn\">

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/"开发者_JAVA百科, "'.$HTTP_SERVER_VARS['HTTP_HOST'].'/directory/list=", $url);// Can not use `$HTTP_SERVER_VARS['HTTP_HOST']` in `preg_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:

"'.$HTTP_SERVER_VARS['HTTP_HOST'].'/directory/list="

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:

$_SERVER['HTTP_HOST']."/directory/list="

This should work. Hope that helps.

As a final note, the alternative option would have ended up with the code looking like this:

"{$_SERVER['HTTP_HOST']}/directory/list="

...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 than preg_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 with str_replace.


Preg uses /'s as a identifyer

$url = preg_replace("/\/docs\//", $HTTP_SERVER_VARS['HTTP_HOST'].'/directory/list=', $url);


You have a strange mix of single and double quotes. Try this instead:

$url = preg_replace("/docs/", $HTTP_SERVER_VARS['HTTP_HOST'].'/directory/list=', $url);
0

精彩评论

暂无评论...
验证码 换一张
取 消