用preg_replace()替换字符

时间:2016-06-18 17:32:58

标签: php regex preg-replace preg-match

使用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);

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

我猜这个网址肯定不会以token开头,而是以其值结尾。您可能会将^$与字词边界(\b)混淆。此外,不需要捕获组,也不需要逃避=

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

答案 1 :(得分:0)

当PHP已经为此目的使用内置函数时,我不会使用正则表来解析引用者:parse_url()parse_str()

遗憾的是,没有内置函数可以从组件部分构建URL,因此用户必须创建自己的URL(最后请参阅Url类的定义)。

更改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课程的简化定义:

/**
 * 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;    
    }
}