0

我有以下正则表达式:

preg_match_all("/.{0,40}<b>(.+?)<\/b>.{0,40}/i", $string, $matches);

目前,它在粗体之前抓取 40 个字符,在粗体之后抓取 40 个字符。我该怎么做才能将其从字符更改为单词。我怎样才能得到它,所以它在粗体之前有 20 个单词,在粗体之后有 20 个单词?

这是一个字符串的示例片段:

使用关键字“in”或“<b>tagged</b>”添加标签。蜘蛛侠这将找到所有包含的结果

4

2 回答 2

1
$str0 = 'Tags are add by using either the keyword "in" or "<b>tagged</b>". Spiderman This will find all results containing';
$len0 = strlen($str0);
preg_match_all('/<b>.+?<\/b>/i', $str0, $matches);  
$str1 = $matches[0][0];
$len1 = strlen($str1);
$len2 = strpos($str0, $matches[0][0]);
$str2 = substr($str0, 0, $len2);
$len3 = $len0 - $len1 - $len2;
$str3 = substr($str0, $len1+$len2, $len3);
$arr1 = array_reverse(explode(' ', $str2));
$arr2 = explode(' ', $str3);
$key1 = count($arr1) - 1;
$key2 = count($arr2) - 1;
$i = 0;
$before = array();
while($i < $key1 && count($before) < 20) {
    if (preg_match('/\w/', $arr1[$i])) {
        $before[] = $arr1[$i];
    }
    $i++;
}
$i = 0;
$after = array();
while($i < $key2 && count($after) < 20) {
    if (preg_match('/\w/', $arr2[$i])) {
        $after[] = $arr2[$i];
    }
    $i++;
}
var_dump($before);
var_dump($after);

虽然代码有点长,但它确实有效:

array:before (size=9)
  0 => string 'or' (length=2)
  1 => string '"in"' (length=4)
  2 => string 'keyword' (length=7)
  3 => string 'the' (length=3)
  4 => string 'either' (length=6)
  5 => string 'using' (length=5)
  6 => string 'by' (length=2)
  7 => string 'add' (length=3)
  8 => string 'are' (length=3)

array:after (size=6)
  0 => string 'Spiderman' (length=9)
  1 => string 'This' (length=4)
  2 => string 'will' (length=4)
  3 => string 'find' (length=4)
  4 => string 'all' (length=3)
  5 => string 'results' (length=7)
于 2013-10-24T02:11:38.733 回答
0

试试这个以获得粗体前后的 20 个单词。

/(?:[^\s]+\s){0,20}<b>(.+?)<\/b>(?:[^\s]+\s){0,20}/i
于 2013-10-24T03:11:22.660 回答