這是我試圖獲得的內容: My longest text to test
當我搜索例如My
我應該得到My longest
獲取當前字符串中的下一個單詞
我試着用這個函數首先得到輸入的完整長度,然後搜索''來剪切它。
$length = strripos($text, $input) + strlen($input)+2;
$stringpos = strripos($text, ' ', $length);
$newstring = substr($text, 0, strpos($text, ' ', $length));
但是這僅適用第一次,然後它的電流輸入後削減,意味着 My lon
是My longest
而不是My longest text
。
我該如何改變這個才能獲得正確的結果,總是會得到下一個單詞。也許我需要休息一下,但我找不到合適的解決方案。
UPDATE
這裏是我的解決辦法,直到我找到一個更好的解決方案。正如我所說的使用數組函數不起作用,因爲部分詞應該工作。所以我延續了我之前的想法。第一次和下一次的基本想法是不同的。我改進了一下代碼。
function get_title($input, $text) {
$length = strripos($text, $input) + strlen($input);
$stringpos = stripos($text, ' ', $length);
// Find next ' '
$stringpos2 = stripos($text, ' ', $stringpos+1);
if (!$stringpos) {
$newstring = $text;
} else if ($stringpos2) {
$newstring = substr($text, 0, $stringpos2);
} }
不漂亮,但嘿它似乎工作^^。無論如何,也許你的某個人有更好的解決方案。
假陽性怎麼樣,你可能在這個短語中有(說)'Mycoplasm',而沒有實際的單詞'My'? –