在字符串中查找某个单词,然后将其环绕

Spe*_* K. 0 php string

我有一个大字符串,超过1000个单词.我需要的是找到一个单词,然后将一些单词包装成变量.

$in = 'This is a very long sentence, what I need is to find the word "phone" in this sentence, and after that, to wrap some words around it';
Run Code Online (Sandbox Code Playgroud)

我如何实现这一目标:

$out = 'find the word "phone" in this sentence';
Run Code Online (Sandbox Code Playgroud)

所以,正如你所看到的,当我找到"电话"这个词时,我想扩展该词的左右两侧.一个真实的例子是,当你在谷歌上进行查询时,如果标题结果如下,你会从网页上获得一些内容,并且查询是粗体.

Dre*_*rew 6

正则表达方式

如果要突出显示字符串中的某些单词(搜索文本),请执行以下操作.

PHP代码:

 $in = 'This is a very long sentence, what I need is to find the word phone in this sentence, and after that, to wrap some words around it';
 $wordToFind  = 'phone';
 $wrap_before = '<span class="highlight_match">';
 $wrap_after  = '</span>';

 $out = preg_replace("/($wordToFind)/i", "$wrap_before$1$wrap_after", $in);

 // value of $out is now: 
 // This is a very long sentence, what I need is to find the word <span class="highlight_match">phone</span> in this sentence, and after that, to wrap some words around it
Run Code Online (Sandbox Code Playgroud)

CSS代码

由于此示例使用span类包装匹配的文本,因此这是必需的示例CSS代码

 <style type="text/css">
    .highlight_match {
        background-color: yellow;
        font-weight: bold;
    }
 </style>
Run Code Online (Sandbox Code Playgroud)


Dav*_*dom 5

这是一种方法。我并不是说这是最好的方法,但是它将起作用。可能有一种正则表达式可以做到“更好”或“更好”。

$in = 'This is a very long sentence, what I need is to find the word phone in this sentence, and after that, to wrap some words around it';
$wordToFind = 'phone';
$numWordsToWrap = 3;

$words = preg_split('/\s+/', $in);
if (($pos = array_search($wordToFind, $words)) !== FALSE) {
  $start = ($pos - $numWordsToWrap > 0) ? $pos - $numWordsToWrap : 0;
  $length = (($pos + ($numWordsToWrap + 1) < count($words)) ? $pos + ($numWordsToWrap + 1) : count($words) - 1) - $start;
  $slice = array_slice($words, $start, $length);
  $out = implode(' ', $slice);
  echo $out;
} else echo 'I didn\'t find it';
Run Code Online (Sandbox Code Playgroud)


dhp*_*tik 5

感谢@DaveRandom 我刚刚改进了代码并重新编写

<?php

$in = 'This is a very long sentence, what I need is to find the word phone in this sentence, and after that, to wrap some words around it';
$wordToFind = 'words';
$numWordsToWrap = 3;
echo $in;
echo "<br />";
$words = preg_split('/\s+/', $in);

$found_words    =   preg_grep("/^".$wordToFind.".*/", $words);
$found_pos      =   array_keys($found_words);
if(count($found_pos))
{
    $pos = $found_pos[0];
}

if (isset($pos)) 
{
    $start = ($pos - $numWordsToWrap > 0) ? $pos - $numWordsToWrap : 0;
    $length = (($pos + ($numWordsToWrap + 1) < count($words)) ? $pos + ($numWordsToWrap + 1) : count($words)) - $start;
    $slice = array_slice($words, $start, $length);

    $pre_start  =   ($start > 0) ? "...":"";    

    $post_end   =   ($pos + ($numWordsToWrap + 1) < count($words)) ? "...":"";

    $out = $pre_start.implode(' ', $slice).$post_end;
    echo $out;
} 
else 
    echo 'I didn\'t find it';
?>
Run Code Online (Sandbox Code Playgroud)

你们都可能喜欢重复使用。

再次感谢 DaveRandom