PHP/MySQL提取字符串中的特定文本段

PHP/MySQL提取字符串中的特定文本段,php,mysql,preg-match,Php,Mysql,Preg Match,请一些有帮助的人告诉我如何提取字符串的特定部分好吗?它们通常是这样的: Includes USD39 bundled airtime, 30 SMSs and 50MB data. Includes USD200 bundled airtime, 110 SMSs and 450MB data. Includes USD399 bundled airtime, 500 SMSs and 650MB data. 我只想展示 USD39 bundled airtime USD200 bundle

请一些有帮助的人告诉我如何提取字符串的特定部分好吗?它们通常是这样的:

Includes USD39 bundled airtime, 30 SMSs and 50MB data.
Includes USD200 bundled airtime, 110 SMSs and 450MB data.
Includes USD399 bundled airtime, 500 SMSs and 650MB data.
我只想展示

USD39 bundled airtime 
USD200 bundled airtime
USD399 bundled airtime
每根绳子上的碎片

我知道这对这里的大多数人来说都是非常容易的,所以道歉:D希望有一天我能问一个更具挑战性的问题

SELECT SUBSTRING(sentence,LOCATE(' ',sentence),LOCATE(' ',sentence,(LOCATE(' ',sentence)+1))-LOCATE(' ',sentence))
FROM(选择“包括39美元捆绑播放时间、30个SMS和50MB数据”作为句子)temp

这应提取39美元,但未检查。希望这能为您获得不同的美元价值,因为第三和第四个价值在每个报表中都是相同的


希望这有帮助

正则表达式

$str = 'Includes USD39 bundled airtime, 30 SMSs and 50MB data.';
$matches = array();
preg_match('/ USD\d+ bundled airtime/i', $str, $matches);
echo '<pre>', print_r($matches, 1), '</pre>';

// more complex:
$matches = array();
preg_match('/ (USD\d+ [^,]+),/i', $str, $matches);
echo '<pre>', print_r($matches, 1), '</pre>';

// another way:
$matches = array();
preg_match('/^Includes ([^,]+),/i', $str, $matches);
echo '<pre>', print_r($matches, 1), '</pre>';
$str='包括39美元的捆绑播放时间、30个SMS和50MB数据';
$matches=array();
preg_match('/USD\d+捆绑播放时间/i',$str,$matches);
回显“”,打印($matches,1),“”;
//更复杂的是:
$matches=array();
preg_match('/(USD\d+[^,]+),/i',$str,$matches);
回显“”,打印($matches,1),“”;
//另一种方式:
$matches=array();
preg_match('/^包括([^,]+),/i',$str,$matches);
回显“”,打印($matches,1),“”;
尝试这样做


同样地,你的答案。(这是一个php函数。希望有帮助。)否则正则表达式将是关键。

您可以发布您尝试过的内容吗?请尝试preg_replace(“/Includes(.*),.*/”,“$1”,“$str”);为什么这个问题被否决了?@2可能是因为OP没有展示他已经尝试过的东西(如果他尝试过的话)。所以这不是一个“请为我编写此代码”的网站。这不是OP想要的。最好将逻辑保留在PHP代码中,而不是Mysql查询本身。
echo substr("Hello world",1,8);