1
$mystring = "@blablabla Kayit Ol ogrencino:1176160"

这是我的字符串,placament of ogrencino:1176160在字符串中可以更改,但其他会稳定。

喜欢:

$mystring = "@blablabla ogrencino:1176160 Kayit Ol" etc.

我该如何解析"1176160"

4

4 回答 4

2

preg_match像这样使用:

$mystring = "@blablabla ogrencino:1176160 Kayit Ol";

// Changed regular expression to match Kolink's comment
preg_match('/(?<=\bogrencino:)\d+/', $mystring, $matches);

print_r($matches);
// displays Array ( [0] => 1176160 )

如果它在字符串中出现多次,您可以执行preg_match_all()

于 2013-06-26T17:13:44.893 回答
1

您还可以查看此正则表达式:

(?<=ogrencino:)(.+?)[\s$]

它与 之后存在的值无关ogrencino:。(可以是数字或非数字)

正则表达式分解:

(?<=ogrencino:) = Positive look behind, checks `ogrencino:` exists behind the next criteria.
.+?             = Any thing (except new line) one or more time, but lazy due to `?`.
[\s$]           = after match either whitespace or end of line will exists.
于 2013-06-26T18:43:30.810 回答
0

您想查看正则表达式,尤其是preg_match

对于您的具体示例,请执行以下操作:

$mystring = "@blablabla ogrencino:1176160 Kayit Ol";
$matches = array();
preg_match( '/ogrencino\d+/', $mystring, $matches);

echo $matches[0];  // ogrencino:1176100

它的作用是找到“一个或多个数字”的每个实例(就是这样\d+)并将每个匹配项读入$matches

于 2013-06-26T17:17:21.993 回答
0

没有正则表达式:

$string = "@blablabla ogrencino:1176160 Kayit Ol";
$beg = strpos($string,"ogrencino:") + strlen("ogrencino:");
$end = strpos($string," ",$beg);
$mynumber = substr($string,$beg,$end-$beg); //1176100
于 2013-06-26T17:20:58.727 回答