如何在php中找到匹配查询的结果

时间:2014-05-14 17:36:45

标签: php mysql regex

我有一个问题无法弄清楚我如何继续下去。我有像

这样的字符串
  "Coolman United States Member Since January 2013 Loans  114.01 Active  66.00 Repaid    Credentials 2 followers eBay windchester 42 Friends 2"

现在用php我需要找到的用户名是" Coolman"国家,会员科学,贷款,活跃,偿还,追随者。

我知道preg_replace,正则表达式我可以找到匹配的确切文本。但问题是有类似模式的数百个数据,不同的用户名,不同的国家,不同的日期等等,所以我怎么能有这样的结果呢?

用户名:Coolman 国家:美国 会员自:2013年1月等等......

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

这可能会对您有所帮助:

function GetBetween($content,$start,$end){
    $r = explode($start, $content);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}

$string="Coolman United States Member Since January 2013 Loans  114.01 Active  66.00 Repaid    Credentials 2 followers eBay windchester 42 Friends 2";
echo GetBetween($string,"Member Since "," Loans");

$wh = strpos($string," ");
$username = substr($string,0,$wh);
$country = substr($string,$wh,strpos($string," Member Since")-$wh);

我在这里假设用户名总是一个字。