tags:

views:

160

answers:

3

Hi my question

Need to get the 10 word before and 10 words after for the given text . i mean need to start the 10 words before the keyword and end with 10 word after the key word.

Given text : "Twenty-three"

The main trick : content having some html tags etc .. tags need to keep that tag with this content only . need to display the words from 10before - 10after

content is bellow :

removed

Thank you

+1  A: 

This method assumes the words are only separated by spaces (not tabs, newlines or other whitespace) and is dependent upon the PHP library function "strip tags" which probably assumes well-formed HTML (which in my experience is a bad assumption).

$string_content = strip_tags($html_content);
$start_cursor = $end_cursor = strpos($string_content, 'Twenty-three');
for($i = 0; $i < 10; $i++) { // rewind backwards until we find 10 spaces
    $start_cursor = strrpos($string_content, ' ', $start_cursor);
}
for($i = 0; $i <= 10; $i++) { // skip forward until we find eleven spaces 
    $end_cursor = strpos($string_content, ' ', $end_cursor);
}
$result_string = substr($string_content, $start_cursor, $end_cursor - $start_cursor);

untested but i believe it's an effective approach

optionally, you can sterilize the whitespace:

$string_content = strip_tags($html_content);
$string_content = preg_replace("/\s+/", " ", $string_content); // replace any number of adjacent whitespace characters with a single space
David
note: this will show fewer than 10 words if adjacent spaces are found. There are slower ways to do this in a more flexible manner if you want...
David
Hi thank you but the "strpos" only can get the position for the first string only its not taken the 'Twenty-three' fully i think so ...
Dam
Hi thanks for the help strip_tags in php same like need to use in mysql i dont want get the row having the keyowrd inside the Html tagsquery WHERE `text` = 'keyword'
Dam
A: 
<?php
$find = 'Twenty-three';
$words = explode(' ', $string);
$wordsLimit = 10; // 10 words

// Number of words
$wordsLength = count($words);

// Find the position of the word ($find) inside the phrase
$findPosition = (in_array($find, $words)) ? array_search($find, $words) : 0;

// Cut the phrase
$beforeIndex = max(0, ($findPosition - $wordsLimit));
$afterIndex = min($wordsLength, ($findPosition + $wordsLimit + 1));
$words = array_slice($words, $beforeIndex, $afterIndex);

// Display the final phrase
$string = join(' ', $words);
echo $words;
?>
TiuTalk
A: 

This should do the trick :

function getSurrounding($string, $needle){
  // Strip html tags
  $string = strip_tags($string);
  // Concat blank characters
  $string = preg_replace('`\\s+`', ' ', $string);
  // Use some regexp magic
  preg_match_all('`(?:[^ ]+ ){10}'.$needle.'(?: [^ ]+){10}`', $string, $blocks);
  return $blocks[0];
}
Arkh