PHP句子搜索

时间:2015-02-19 18:06:48

标签: php search

是否可以通过输入第一个和最后一个单词来搜索句子?

实施例: 我们有一个句子" Hello world这是一个例子,这里是第一个单词,或者我需要从这里开始的句子,这里是最后一个单词,这意味着这个单词之前的所有内容都不是所需"

我想得到什么: "第一个字,或者我需要从这里开始的一个句子的开头,这是最后的"。

像这样,我需要在PHP中。

我的想法是用一些数组来做这个,它开始将单词从第一个单词保存到最后一个单词。

1 个答案:

答案 0 :(得分:1)

问题没有任何努力,但有点有趣。这是一种无聊的老式方式

<?php

$string="Hello world this is an example and here is FIRST word,
or the start of a sentence that I need to take from here, 
and here is the LAST word, what means that everything 
before this word is not needed";

$first="FIRST";
$last="LAST";

$string=substr($string,strpos($string,$first));                 // Cut Left
$string=substr($string,0,strpos($string,$last)+strlen($last)); //  Cut Right

echo $string;

<强>输出

FIRST word, or the start of a sentence that I need to take from here, 
and here is the LAST

但既然你没有付出任何努力,不要期待解释和/或更好的方式与正则表达式:)