2012-01-23 30 views
0

我有一个大字符串,超过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'; 

所以,你可以看到,当我找到单词“手机”,我想这个词的左向右&扩大。 一个真实的例子是,当您在google上查询标题结果时,您会从网页获取一些内容,并且查询以粗体显示。

+0

您的匹配有多模糊?精确匹配,还是与词干和这样的规范化?你有搜索查询的位置数据吗? –

+0

请确定一个*词*是什么。 – hakre

回答

4

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

$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'; 
+0

谢谢,这个伎俩。 –

2

这是你如何能做到的是一个相对简单的例子:

<?php 

    $in = "blah blah blah test blah blah blah"; 
    $search = "test"; 
    $replace = "--- test ---"; 

    $out = str_replace($search, $replace, $in); 

?> 
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); 

以匹配电话中的“hon”

+0

'\ W == [^ \ w]' - 可能会使模式更具可读性。关于['preg_quote'](http://php.net/preg_quote)的说明也可能有用。 – hakre

+0

@hakre对不起,我很愚蠢,但我没有承诺你的评论 –

+0

你可以用'\ W +'替换每个'[^ \ w] +'。可以使用'preg_quote'来插入搜索项,以确保模式不会中断。 – hakre

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单词。

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代码

由于这个例子是包装,跨度类匹配的文本,这里是强制性例如CSS代码

<style type="text/css"> 
    .highlight_match { 
     background-color: yellow; 
     font-weight: bold; 
    } 
</style> 
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

相关问题