用 preg_replace() 替换字符

Replace characters with preg_replace()

使用 preg_match() 我想在字符串中匹配以下事件:

token=[and here 32 characters a-z0-9]

例如:

token=e940b20a98d01d08d21a919ecd025d90

我试过了

/^(token\=)[a-z0-9]{32}$/

但是没有用!

我想做的是:

$referer = $_SERVER['HTTP_REFERER'];
$referer = preg_replace("/^(token=)[a-z0-9]{32}$/","token=".token(),$referer);

感谢您的帮助!

我想 url 不一定以 token 开头并以其值结尾。您可能将 ^$ 与单词边界 (\b) 混淆了。此外,不需要捕获组,也不需要转义 =.

/\btoken=[a-z0-9]{32}\b/

当 PHP 已经具有用于此目的的内置函数时,我不会使用正则表达式解析引用:parse_url()parse_str().

不幸的是,没有内置函数可以从组件中构建 URL,因此用户必须自己发明(请参阅最后的 Url class 的定义).


更改 token 查询参数:

// parse the referer
$components = Url::parse($_SERVER['HTTP_REFERER']);

// replace the token
$components['query']['token'] = token();

// rebuild the referer
$referer = Url::build($components);

这是我的 Url class 的简化定义:

/**
 * Url parsing and generating utility
 * 
 * @license https://opensource.org/licenses/MIT MIT License
 * @author ShiraNai7               
 */   
final class Url
{
    /**
     * @param string $url
     * @return array
     */              
    static function parse($url)
    {
        $components = parse_url($url);
        if (false === $components) {
            throw new \InvalidArgumentException('Invalid URL');
        }

        if (isset($components['query'])) {
            parse_str($components['query'], $components['query']);
        }

        return $components + array(
            'scheme' => null,
            'host' => null,
            'port' => null,
            'user' => null,
            'pass' => null,
            'path' => null,
            'query' => array(),
            'fragment' => null,
        );
    }

    /**
     * @param array $components
     * @param bool  $absolute     
     * @return string     
     */         
    static function build(array $components, $absolute = true)
    {
        $output = '';

        if ($absolute) {
            if (empty($components['host']) || empty($components['scheme'])) {
                throw new \LogicException('Cannot generate absolute URL without host and scheme being set');
            }

            if (!empty($components['scheme'])) {
                $output .= $components['scheme'] . '://';
            }

            if (!empty($components['user'])) {
                $output .= $components['user'];
            }
            if (!empty($components['pass'])) {
                $output .= ':' . $components['pass'];
            }
            if (!empty($components['user']) || !empty($components['pass'])) {
                $output .= '@';
            }

            $output .= $components['host'];

            if (!empty($components['port'])) {
                $output .= ':' . $components['port'];
            }
        }

        if (!empty($components['path'])) {
            $output .= (($components['path'][0] !== '/') ? '/' : '') . $components['path'];
        } else {
            $output .= '/';
        }

        if (!empty($components['query'])) {
            $output .= '?';
            $output .= http_build_query($components['query'], '', '&');
        }

        if (!empty($components['fragment'])) {
            $output .= '#' . $components['fragment'];
        }

        return $output;    
    }
}