Php字符串拆分,我该怎么做?

时间:2013-06-26 17:11:23

标签: php parsing split

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

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

像:

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

我如何解析"1176160"

4 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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.

答案 2 :(得分:0)

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

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

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

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

这样做是找到“一个或多个数字”的每个实例(\d+都是这样)并将每个匹配读到$matches

答案 3 :(得分:0)

没有REGEX:

$string = "@blablabla ogrencino:1176160 Kayit Ol";
$beg = strpos($string,"ogrencino:") + strlen("ogrencino:");
$end = strpos($string," ",$beg);
$mynumber = substr($string,$beg,$end-$beg); //1176100