能否通过输入第一个单词和最后一个单词来搜索一个句子?
例句:我们有一个句子“你好世界--这是一个例子,这里是第一个词,或者是我需要从这里开始的一个句子的开头,这是最后一个词,意思是这个词之前的一切都是不需要的”。
我想得到的是:“第一个词,或者我需要从这里取下来的句子的开头,这里是最后一个”。
类似这样的东西,我需要它在PHP中。
我的想法是用一些数组来实现这一点,它开始从第一个单词到最后一个单词保存单词。
发布于 2015-02-19 18:27:41
问题没有任何努力,但有点有趣。这是一种乏味的老式方式。
<?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但是,既然你没有付出任何努力,就不要指望一个解释和一个更好的方法来使用regexes :)
https://stackoverflow.com/questions/28613716
复制相似问题