gpt4 book ai didi

php - 使用 php 变量的 preg_match 问题

转载 作者:行者123 更新时间:2023-12-02 07:05:03 25 4
gpt4 key购买 nike

我有一个变量,我想在 preg_match 中结合一些正则表达式使用:

$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-/d.*/", $string)) {
echo "matched";
}

在我的模式中,我尝试使用奶酪进行匹配,然后是 - 和 1 数字,然后是其他任何内容。

最佳答案

  1. /d改为\d
  2. 不需要使用.*
  3. 如果您的字符串是由用户定义的(或者可能包含一些字符(例如:/*...))可能会导致您的比赛出现问题。

代码:

<?php
$string = "cheese-123-asdf";
$find = "cheese";
if(preg_match("/$find-\d/", $string))
{
echo "matched";
}
?>

关于php - 使用 php 变量的 preg_match 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13868125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com