extract part of a string before and after a word

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

i need to extract and show some words before and after a query word, something like google search results, for example:

$str = "hi user! welcome to new php open source world, we are trying to learn you something!"; $query = "new php"; $result = "... welcome to new php open source ...";

i searched google an SO but didn't find a clear answer or maybe my php knowledge was not enough! is there a workable and easy-to-use function to do this job?

回答1:

function yourFuncName($str, $query, $numOfWordToAdd) {     list($before, $after) = explode($query, $str);      $before = rtrim($before);     $after  = ltrim($after);      $beforeArray = array_reverse(explode(" ", $before));     $afterArray  = explode(" ", $after);      $countBeforeArray = count($beforeArray);     $countAfterArray  = count($afterArray);      $beforeString = "";     if($countBeforeArray < $numOfWordToAdd) {         $beforeString = implode(' ', $beforeArray);     }     else {         for($i = 0; $i < $numOfWordToAdd; $i++) {             $beforeString = $beforeArray[$i] . ' ' . $beforeString;          }     }      $afterString = "";     if($countAfterArray < $numOfWordToAdd) {         $afterString = implode(' ', $afterArray);     }     else {         for($i = 0; $i < $numOfWordToAdd; $i++) {             $afterString = $afterString . $afterArray[$i] . ' ';          }     }      $string = $beforeString . $query . ' ' . $afterString;      return $string; }

Output is: user! welcome to new php open source world, ($numOfWordToAdd = 3)



回答2:

Here is an working example I thing that it is clear what I did and how:

<?php  $str = "hi user! welcome to new php open source world, we are trying to learn you something!"; $query = "new php";      $expl = explode($query, $str);      // items on the left side of middle string     $expl_left = explode(" ", $expl[0]);      $left_cnt = count($expl_left);      $new_left = $expl_left[$left_cnt-3] . " " . $expl_left[$left_cnt-2];      // items on the right side of middle string     $expl_right = explode(" ", $expl[1]);      $new_right = $expl_right[1] . " " . $expl_right[2];      // new string formated     $new = "... " . $new_left . " " . $query . " " . $new_right . " ...";  print $new;  ?>

If you have some questions feel free to ask...



回答3:

$result = preg_replace('/(.+)?([^\s]+.{10}'.$query.'.{10}[^\s]+)(.+)?/', '... $2 ...', $str);

This will return the same result from the same string and query you gave. If the before or after length starts or ends (respectively) in the middle of a word, it will continue until it completes the word before it stops.



回答4:

Assuming a "word" is any series of non-whitespace characters, the following will extract 3 words on either side of new php out of the string $subject, but accept less if necessary:

if (preg_match('/(?:\S+\s+){1,3}new php(?:\s+\S+){1,3}/', $subject, $regs)) {     $result = $regs[0]; }

Change the 3s to any number you like.



回答5:

I used the following function with explode:

public static function returnSearch($query, $str, $wordcount) {     $explode = explode($query, $str);     $result = null;     //if explode count is one the query was not found     if (count($explode) == 1) {         $result = implode(' ', array_slice(str_word_count($explode[0], 2), -$wordcount, $wordcount)) . " ";     }     //if explode count is more than one the query was found at least one time     if (count($explode) > 1) {         //check for if the string begins with the query         if (!empty($explode[0])) {             $result =  "..." . implode(' ', array_slice(str_word_count($explode[0], 2), -$wordcount, $wordcount)) . " ";         }         $result = $result . $query;         if (!empty($explode[1])) {             $result = $result . " " . implode(' ', array_slice(str_word_count($explode[1], 2), 0, $wordcount)) . "...";         }     }     //return result     return $result; }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!