Bu yöntem kelime sadece boşluk (değil sekmeler, satırsonlarının veya başka boşlukla) tarafından ayrılır varsayar ve muhtemelen (benim deneyim kötü bir varsayım) iyi biçimli HTML varsayar PHP kütüphane işlevi "strip etiketleri" bağlıdır.
$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);
denenmemiş ama etkili bir yaklaşım olduğuna inanıyorum
isteğe bağlı, Eğer boşluk sterilize edebilirsiniz:
$string_content = strip_tags($html_content);
$string_content = preg_replace("/\s+/", " ", $string_content); // replace any number of adjacent whitespace characters with a single space