Get Off My Lawn
Get Off My Lawn

Reputation: 36299

Convert Regular Expression to get words instead of characters

I have the following regular expression:

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

Currently as it is, it grabs 40 characters before the bold, and 40 characters after the bold. What can I do to change it from characters to words. How can I get is so it gets 20 words before the bold and 20 words after the bold?

Here is an example snippet of a string:

Tags are add by using either the keyword "in" or "<b>tagged</b>". Spiderman This will find all results containing

Upvotes: 0

Views: 112

Answers (2)

Narendra Yadala
Narendra Yadala

Reputation: 9664

Try this to get 20 words before and after bold.

/(?:[^\s]+\s){0,20}<b>(.+?)<\/b>(?:[^\s]+\s){0,20}/i

Upvotes: 0

Eathen Nutt
Eathen Nutt

Reputation: 1473

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

Although it is kinda long code, but it really works:

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)

Upvotes: 1

Related Questions