简体   繁体   English

从字符串中提取数字和斜杠

[英]Extract numbers and slashes from string

I have the following text string: 我有以下文本字符串:

201408/ebrev_50000_20140804.pdf

I want to do an if-statement that checks if the string contains the date number and the slash: xxxxxx/ in the string. 我想做一个if语句,检查字符串是否包含日期数字和斜杠:xxxxxx /。 How can I solve this? 我该如何解决?

if (preg_match("@^\d+/@", $string))

^ is a zero-width anchor to the start of the string. ^是字符串开头的零宽度锚点。 \\d+ is one or more digits. \\d+是一个或多个数字。 / is a literal slash. /是文字斜线。 You can use different PCRE delimiters so you don't have to escape the slash. 您可以使用不同的PCRE分隔符,因此不必逃脱斜线。

If you want to get the actual current date, you can use date("Ym") , which is four-digit year and two-digit month with leading zero. 如果要获取实际的当前日期,可以使用date("Ym") ,它是四位数的年份和两位数的月份,前导零。 Then you wouldn't actually need to use PCRE because you have the literal string you need to match. 然后,您实际上不需要使用PCRE,因为您具有需要匹配的文字字符串。

if (strpos(date("Ym") + "/", $string) === 0)

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

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