我有一个大字符串,超过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';
所以,正如你所看到的,当我找到" phone"这个词时,我想在左边和右边展开。这个词的权利。 一个真实的例子是,当您在谷歌上进行查询时,如果标题结果如下,您会从网页上获得一些内容,并且查询会以粗体显示。
答案 0 :(得分:5)
正则表达方式
如果要突出显示字符串中的某些单词(搜索文本),请执行以下操作。
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>
答案 1 :(得分:4)
这是一种方式。我不是说这是最好的方式,但它会起作用。可能有一种正则表达方式可以“更好”或“更好”。
$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';
答案 2 :(得分:4)
感谢@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
答案 3 :(得分:2)
这是一个相对简单的例子,说明如何实现这一目标:
<?php
$in = "blah blah blah test blah blah blah";
$search = "test";
$replace = "--- test ---";
$out = str_replace($search, $replace, $in);
?>
答案 4 :(得分:2)
$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);
匹配&#34; hon&#34;在电话中
答案 5 :(得分:0)
$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单词。