获取两个子字符串之间的子字符串[重复]

问题描述 投票:0回答:1

我有一个字符串,我想为其提取一些信息。 字符串可能是这样的

$string = "Followers: abc.com. ID by: [email protected]. More info: all the rest of information goes here. All other goes everywhere else."

$string 有时只有

$string = "ID by: [email protected]."

$string = "Followers: abc.com."

或任何其他组合。我只是想看看那里是否有身份证并把它拿出来。

实现这一目标的最佳方法是什么?

php regex text-extraction
1个回答
2
投票

使用带有

preg_match
的正则表达式来查找 ID。

preg_match('/ID by\\: ([\\w@\\.]+)\\.(?: |$)/',$input,$matches);

ID(电子邮件地址)将在

$matches[1]
中。该模式匹配“ID by:”,捕获电子邮件地址,最后需要句点+空格或字符串结尾。

© www.soinside.com 2019 - 2024. All rights reserved.