【问题标题】:PHP sentence searchPHP 句子搜索
【发布时间】:2015-02-20 02:33:07
【问题描述】:

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

示例: 我们有一个句子“Hello world this is an example and here is FIRST word, or the start of a sentence that I need from here, and here is the LAST word,这意味着不需要这个词之前的所有内容”。

我想得到什么: “第一个词,或者我需要从这里取的句子的开头,这里是最后一个”。

类似这样的东西,我在 PHP 中需要它。

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

【问题讨论】:

  • 在哪里搜索?在数据库中?在一个字符串中?
  • 你研究过正则表达式吗?对于在 PHP 和许多其他语言中进行此类搜索非常有用。 regexone.com/cheatsheet
  • 句子将从数据库中取出,作为一个字符串
  • 我正在寻找最简单、最轻便的方法来实现这个

标签: php search


【解决方案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

但是由于您没有付出任何努力,所以不要指望使用正则表达式的解释和/或更好的方法:)

【讨论】:

  • 抱歉描述,我的英语真的很糟糕 xD 我在这里只需要一个想法,所以我不希望有一个很好的解释和谢谢
猜你喜欢
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2011-10-30
  • 2011-04-09
相关资源
最近更新 更多