doudi2431
2018-10-23 09:11
浏览 50
已采纳

从字符串php codeigniter获取指定单词

I have an input for search ,

i search for "res"

<input type="text" name="search"I/>

i use Like method to get search results,

$txt = $this -> input -> post("search");
$this -> db -> like('title', $txt);
if ($sql = $this -> db -> get('ads') -> result()) {
            $data['posts'] = $sql;

            print_r($sql);

        }

and get this results :

some text restaurant and other
some text result
my resume 

i want to show the liked word extract the word from strings and remove other texts

i want this :

restaurant
result
resume

图片转代码服务由CSDN问答提供 功能建议

我有一个搜索输入,

我搜索“res”

 &lt; input type =“text”name =“search”I /&gt; 
   
 
 

我使用 与获取搜索结果的方法类似,

  $ txt = $ this  - &gt; 输入 - &gt; 发布(“搜索”); 
 $ this  - &gt;  db  - &gt; 喜欢('title',$ txt); 
if($ sql = $ this  - &gt; db  - &gt; get('ads') - &gt; result()){
 $ data ['posts'] = $  sql; 
 
 print_r($ sql); 
 
} 
   
 
 

并得到以下结果:

  一些文本餐厅和其他
some文本结果
my resume 
   
 
 

我想显示喜欢的单词 extract字符串中的单词并删除其他文本< / p>

我想要这个:

  restaurant 
result 
resume 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doudun3910 2018-10-23 09:34
    最佳回答

    You can split the found string into an array (explode) and then search this array with the given string (preg_grep) and output the found strings.

    <?php
    $searchString = "/res/";
    $foundString = "some text restaurant and other";
    $explode = explode(" ", $foundString);
    $searched = preg_grep($searchString, $explode);
    
    foreach($searched as $item) {
      echo $item . " ";
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题