我的字符串中列出了一年
$s = "Acquired by the University in 1988";
Run Code Online (Sandbox Code Playgroud)
在实践中,这可能是这个单行字符串中的任何位置.如何使用regexr提取它?我尝试过\ d但是没有用,它只是出现了一个错误.
贾森
我在LAMP 5.2中使用了preg_match
rid*_*ner 13
你需要一个正则表达式来匹配四个数字,这四个数字必须包含一个完整的单词(即10个数字的字符串包含四个数字,但不是一年.)因此,正则表达式需要包括像这样的单词边界:
if (preg_match('/\b\d{4}\b/', $s, $matches)) {
$year = $matches[0];
}
Run Code Online (Sandbox Code Playgroud)