好的,所以我想知道如何存储 preg_match,仅当字符串本身具有匹配的 preg_match 时才匹配到变量中。
例如 :
$string1 = "this is some example id=34 ";
preg_match('/^id=(\d+)/', $string1);
我只想将 id=34 或 id=somenumber 存储到一个变量中,前提是 $string1 包含 id=somenumber我想在这种情况下
好的,所以我想知道如何存储 preg_match,仅当字符串本身具有匹配的 preg_match 时才匹配到变量中。
例如 :
$string1 = "this is some example id=34 ";
preg_match('/^id=(\d+)/', $string1);
我只想将 id=34 或 id=somenumber 存储到一个变量中,前提是 $string1 包含 id=somenumber我想在这种情况下
你读过文档吗?他们会告诉你preg_match()
接受第三个论点:
preg_match('/^id=(\d+)/', $string1, $matches);
之后,$matches[1]
将包含匹配的 ID ( '43'
) 和$matches[0]
匹配的整个字符串(即'id=34'
在您的示例中)。