foreach ($section->getElements() as $element) {
if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) {
foreach ($element->getElements() as $text) {
if ($text instanceof \PhpOffice\PhpWord\Element\Text) {
$lectureTitle = $text->getText();
break 2; // Found the title, break both loops
}
}
} elseif ($element instanceof \PhpOffice\PhpWord\Element\Text) {
$lectureParagraphs[] = $element->getText();
}
}
}
// Get the search term from the form
$searchTerm = $_POST[‘search_term’];
// Output paragraphs containing the search term followed by the lecture title
foreach ($lectureParagraphs as $paragraph) {
if (stripos($paragraph, $searchTerm) !== false) {
echo ”
$lectureTitle: $paragraph
“;
}
}
}
?>
Word Document Search