简体   繁体   中英

get specify word from string 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

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 . " ";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM