我有一个大字符串,有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';
我如何做到这一点:
$out = 'find the word "phone" in this sentence';
所以,正如你所看到的,当我找到"电话"这个词时,我想在左边展开&这个词的正确性。一个真实的例子是,当你在谷歌上进行查询时,在标题结果下方,你会从网页中获得一些内容,并且查询是粗体的。
Regex方式
如果要突出显示字符串中的某些单词(搜索文本),请执行以下操作。
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
CSS代码
由于这个例子是用一个span类包装匹配的文本,这里是必须的例子CSS代码
<style type="text/css">
.highlight_match {
background-color: yellow;
font-weight: bold;
}
</style>
这里有一种的方法。我不是说这是最好的方法,但它会起作用。可能有一种regex方法可以做到这一点,它会"更好"或"更好"。
$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';
感谢@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';
?>
你们可能都喜欢重复使用。
再次感谢DaveRandom
这是一个相对简单的例子,说明了如何实现这一点:
<?php
$in = "blah blah blah test blah blah blah";
$search = "test";
$replace = "--- test ---";
$out = str_replace($search, $replace, $in);
?>
$out=preg_match('/'w+'s+'w+'s+'w+'s+'"phone'"'s+'w+'s+'w+'s+'w+/',$in,$m);
if ($out) $out=$m[0];
如果引号是可选的,并且您希望在特殊字符方面具有圆顶灵活性,请使用
preg_match('/'w+[^'w]+'w+[^'w]+'w+[^'w]+phone[^'w]+'w+[^'w]+'w+[^'w]+'w+/',$in,$m);
如果你想匹配部分单词,请使用
preg_match('/'w+[^'w]+'w+[^'w]+'w+[^'w]+'w*hon'w*[^'w]+'w+[^'w]+'w+[^'w]+'w+/',$in,$m);
匹配手机中的"hon"
$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';
$array = explode(" ", $in);
$how_much = 3;
$search_word = "phone";
foreach ($array as $index => $word) {
if ($word == $search_word) {
for ($index1 = 0; $index1 < ($how_much * 2) + 1; $index1++) {
$key = $index-$how_much+$index1;
echo $array[$key];
echo " ";
}
}
}
这是一个简单的解决方案。在空格上分解句子,然后在两个方向上显示单词+$how_much单词。