[英]find string with regex in php
I am new to regex and therefore i gone through php manual for regex and tried the below: 我是regex的新手,因此我通过了regex的php手册并尝试了以下方法:
i have a string like this: 我有一个像这样的字符串:
c-1
c-10
c-100
and so on upto c-unlimited
c-100
,依此类推,直到c-unlimited
now i want to confirm that the string coming is a valid string which starts with c-
and after c-
it only contains numbers.. 现在我想确认来的字符串是与开头的有效字符串
c-
后c-
它仅包含数字..
to get this work i have used preg_match
为了完成这项工作,我使用了
preg_match
$slug='c-12';
if(preg_match("/\[c-(.*?)]/",$slug,$result)){ echo "TRUE";}
else{ echo "FALSE"; }
also this: 还有这个:
$pattern = '/^c-/';
if(preg_match($pattern, substr($slug,2), $matches, PREG_OFFSET_CAPTURE){ echo "TRUE";}
else{ echo "FALSE"; }
and also this: 还有这个:
$pattern = '/^c-/';
if(preg_match($pattern, $slug, $matches, PREG_OFFSET_CAPTURE, 3){ echo "TRUE";}
else{ echo "FALSE"; }
and one more thing i also wanted to to validate the string from end. 还有一件事,我还想从结尾验证字符串。 like below:
如下所示:
$slug="my-string-content-123";
here i wanted to validate that the string contains the number at end after -
在这里,我想验证字符串是否在末尾包含数字
-
example 123
例子
123
but i am not able to get it work... and i am sorry for my bad english.. 但我无法使其正常工作。。。我为我的英语不好而感到抱歉。
any help or suggesstion would be a great help.. thanks in advance.. 任何帮助或建议将是一个很大的帮助..在此先感谢..
For common (not just c-
) case: 对于常见的情况(不只是
c-
):
$slug="my-string-content-123";
if(preg_match('/([^0-9]+)-([0-9]+)$/', $slug, $matches))
{
//prefix is in $matches[1];
//the number is in $matches[2];
}
Try this: 尝试这个:
preg_match("/^c-\d+$/",$slug,$result);
^c-
= start with c-
^c-
=以c-
开头
\\d+
= 1 or more digits \\d+
= 1个或更多数字
$
= end of string $
=字符串结尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.