Preg_Match一个文本框提交

时间:2018-04-30 13:59:21

标签: php

我想提交一个简单的文本框,在preg_match之后返回一个值? 例如,

我会进入文本框: 你在他家,厨房找到了卢克

我想提取"房子,厨房"另外preg_match呢?不知道怎么做

2 个答案:

答案 0 :(得分:0)

如果这是你想要完成的事情:

preg_match('/\b(kitchen\w+)\b/', $string, $matches); // matches kitchen
preg_match('/\b(house\w+)\b/', $string, $matches); // matches kitchen

您可以为其他匹配添加通配符。
如需了解更多信息,请参阅:
PHP preg_match reference.

答案 1 :(得分:0)

$str = "You found luke in his house, kitchen";

如果单词组合始终位于字符串的末尾,则可以使用explode()

$words = explode(" ", $str);
$nWords = count($words);
$combo = (strpos($words[$nWords-1], ",") !== false)?$words[$nWords-1]." ".$words[$nWords]:'incorrect form'; // only when , is in second last 'word'

如果您事先了解所有组合,则可以选择:

$knownCombos = array('house, kitchen', 'house, bathroom', 'house, basement');
foreach($knownCombos as $i => $search) {
    if(strpos($str, $search) !== false) {
        $combo = $search;
        break;
    }
}