Php 如何从字符串中提取ID号?

Php 如何从字符串中提取ID号?,php,regex,substr,delimited-text,Php,Regex,Substr,Delimited Text,如何使用regex或preg_match检索中间值 $str = 'fxs_124024574287414=base_domain=.example.com; datr=KWHazxXEIkldzBaVq_of--syv5; csrftoken=szcwad; ds_user_id=219132; mid=XN4bpAAEAAHOyBRR4V17xfbaosyN; sessionid=14811313756%12fasda%3A27; rur=VLL;' 我如何只获取值​​从使用正则表达式或p

如何使用regex或preg_match检索中间值

$str = 'fxs_124024574287414=base_domain=.example.com; datr=KWHazxXEIkldzBaVq_of--syv5; csrftoken=szcwad; ds_user_id=219132; mid=XN4bpAAEAAHOyBRR4V17xfbaosyN; sessionid=14811313756%12fasda%3A27; rur=VLL;'
我如何只获取值​​从使用正则表达式或preg\u match的ds\u用户id?

如果我们希望使用explode:

$str = 'fxs_124024574287414=base_domain=.example.com; datr=KWHazxXEIkldzBaVq_of--syv5; csrftoken=szcwad; ds_user_id=219132; mid=XN4bpAAEAAHOyBRR4V17xfbaosyN; sessionid=14811313756%12fasda%3A27; rur=VLL;'
输出 在这里,我们还可以使用两个非捕获组和一个捕获组:

(?:ds_user_id=)(.+?)(?:;)
我们有一个左边界:

(?:ds_user_id=)
(?:;)
和一个右边界:

(?:ds_user_id=)
(?:;)
我们通过以下方式收集我们想要的数字或任何我们想要的东西:

(.+?)
如果我们希望验证我们的ID号,我们可以使用:

(?:ds_user_id=)([0-9]+?)(?:;)
我们所需的值可以简单地使用var_dump$matches[0][1];调用

测验 输出
好的,没有什么能比得上mickmackusa\K结构。 但是,对于受损的引擎,这是第二个最好的选择

\d?使用preg\u match来匹配ds\u user\u id=,然后用\K忘记那些匹配的字符,然后匹配一个或多个数字。没有捕获组,没有查找,没有解析所有键值对,没有分解

代码:

输出:

219132

你必须使用正则表达式吗?使用explode可能更容易$数组=分解“;”$str;我应该执行什么命令来获取特定值?虽然有点假,但将其转换为参数字符串,然后对其进行解析将得到一个值数组-parse_strstrstr_replace;,&$str,$params;然后回显$params['ds_user_id'];请要求海报在回答之前尝试一些东西。当这个社区作为一个免费的代码编写服务被滥用时,更多的给我codez问题是被鼓励的。此外,当需要分解正则表达式模式的返回值时,应该对模式进行优化。引号块格式用于引号文本。在Stackexchange网站上,引用格式通常用于错误消息和文档中的精确引用。请参阅我的答案,了解如何简单地编写此任务。我知道你比这篇文章更好。是否有其他人在您的帐户中发布答案?每次遇到一系列数字时,您都在双向搜索。我希望有sln。
 (                          # (1 start), Consume many ID digits
      \d                         # First digit of ID
      (?<= ds_user_id= \d )      # Look behind, assert ID key exists before digit
      \d*                        # Optional the rest of the digits
 )                          # (1 end)
 (?= ; )                    # Look ahead, assert a colon exists
 (                             # (1 start), Consume many ID digits
      \d                            # First digit of ID
      (?:
           (?<! ds_user_id= \d )         # Look behind, if not ID,
           \d*                           # get rest of digits
           (*SKIP)                       # Fail, then start after this
           (?!)
        |  
           \d*                           # Rest of ID digits
      )
 )                             # (1 end)
 (?= ; )                       # Look ahead, assert a colon exists
Regex1:   (\d(?:(?<!ds_user_id=\d)\d*(*SKIP)(?!)|\d*))(?=;)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   1
Elapsed Time:    0.53 s,   534.47 ms,   534473 µs
Matches per sec:   93,550


Regex2:   (\d(?<=ds_user_id=\d)\d*)(?=;)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   1
Elapsed Time:    0.80 s,   796.97 ms,   796971 µs
Matches per sec:   62,737


Regex3:   ds_user_id=\K\d+(?=;)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   1
Elapsed Time:    0.21 s,   214.55 ms,   214549 µs
Matches per sec:   233,046


Regex4:   ds_user_id=(\d+)(?=;)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   1
Elapsed Time:    0.23 s,   231.23 ms,   231233 µs
Matches per sec:   216,232
$str = 'fxs_124024574287414=base_domain=.example.com; datr=KWHazxXEIkldzBaVq_of--syv5; csrftoken=szcwad; ds_user_id=219132; mid=XN4bpAAEAAHOyBRR4V17xfbaosyN; sessionid=14811313756%12fasda%3A27; rur=VLL;';
echo preg_match('~ds_user_id=\K\d+~', $str, $out) ? $out[0] : 'no match';
219132