简体   繁体   English

来自URL字符串的preg_match

[英]preg_match from URL string

I have a string passed through a campaign source that looks like this: 我有一个通过广告系列来源传递的字符串,如下所示:

/?source=SEARCH%20&utm_source=google&utm_medium=cpc&utm_term=<keyword/>&utm_content={creative}&utm_campaign=<campaign/>&cpao=111&cpca=<campaign/>&cpag=<group/>&kw=<mpl/>

when its present I need to cut this up and pass it through to our form handler so we can track our campaigns. 当它现在我需要切断它并将其传递给我们的表单处理程序,以便我们可以跟踪我们的活动。 I can check for it, hold its contents in a cookie and pass it throughout our site but i am having and issue using preg_match to cut this up and put it into variables so I can pass their values to the handler. 我可以检查它,将其内容保存在cookie中并将其传递到我们的网站,但我正在使用preg_match将其剪切并将其放入变量,以便将其值传递给处理程序。 I want the end product to look like: 我希望最终产品看起来像:

$utm_source=google;
$utm_medium=cpc;
$utm_term=<keyword/>

there is no set number of characters, it could be Google, Bing etc, so i am trying to use preg_match to get the first part (utm_source) and stop past what I want (&) and so forth but I don't understand preg_match well enough to do this. 没有设定数量的字符,可能是Google,Bing等,所以我试图使用preg_match来获取第一部分(utm_source)并停止超出我想要的内容(&)等等但我不明白preg_match这足以做到这一点。

PHP should be parsing your query sting for you, into $_GET . PHP应该将你的查询解析为$_GET Otherwise, PHP knows how to parse query strings. 否则,PHP知道如何解析查询字符串。 Don't use regular expressions or for this, use parse_str . 不要使用正则表达式或为此使用parse_str

Input: 输入:

<?php

$str = "/?source=SEARCH%20&utm_source=google&utm_medium=cpc&utm_term=<keyword/>&utm_content={creative}&utm_campaign=<campaign/>&cpao=111&cpca=<campaign/>&cpag=<group/>&kw=<mpl/>";

$ar = array();
parse_str($str, $ar);
print_r($ar);

Output: 输出:

Array
(
    [/?source] => SEARCH
    [utm_source] => google
    [utm_medium] => cpc
    [utm_term] => <keyword/>
    [utm_content] => {creative}
    [utm_campaign] => <campaign/>
    [cpao] => 111
    [cpca] => <campaign/>
    [cpag] => <group/>
    [kw] => <mpl/>
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM