PHP在php中找到模式

时间:2014-07-25 11:24:28

标签: php

我有一个像

这样的字符串
$str="with a little love this home can be hud case # 351-377554. property sold as-is. information from sources deemed reliable but not guaranteed. buyer to verify all information. streamline k eligible.";

这里我需要# 351-377554

2 个答案:

答案 0 :(得分:1)

使用正则表达式。

<?php

$str="with a little love this home can be hud case # 351-377554. property sold as-is. information from sources deemed reliable but not guaranteed. buyer to verify all information. streamline k eligible.";

preg_match("/# [0-9]{3}-[0-9]+/", $str, $matches);

print_r( $matches );

答案 1 :(得分:0)

如果数字始终采用相同的格式(即以#开头并以。结尾),那么您可以使用:

$exploded1 = explode('#', $str);
$exploded2 = explode('.', $exploded1[1]);
echo $exploded2[0]; //will echo number

你可以check explode function here.