PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$count = preg_match_all("/.*$search.*/i",$text,$matches);
foreach ($matches as $key)
{
for ($b = 0;$b < $count;$b++)
echo "RESULT: ".$key[$b]."<br /><br />\n";
}
This script searches $text for a $search and then it returns the lines of $matches. I would need it to echo the sentences, not lines. How is this possible?
$text = "here goes some text. another sentence. here it should be searched. it should be searched by sentences not by lines. it should echo the whole sentence as result not a line!";
<?php
$text = "here is a sentence. here's another one. ah, another one.";
$searchKeyword = "here";
// split $text into an array of sentences using explode
$sentences = explode(".", $text);
// cycle through each of the sentences
foreach($sentences as $key => $value) {
if(strpos($text, $searchKeyword) === true) { // test to see if the $searchKeyword exists in this $value using strpos()...
echo trim($value) . '.\n'; // ... and if it does, trim off the sentence's leading and trailing whitespace and print it
}
}
/*
will print:
here is a sentence.
here's another one.
*/
?>
$count = preg_match_all("/[.!?][^.!?]*".$search."[^.!?]*[.!?]/i",$text,$matches);
foreach ($matches as $key)
{
for ($b = 0;$b < $count;$b++)
{
$result = preg_replace("/^[.!?]\s*/","",$key[$b]); // THIS DELETES THE . ! AND ? IN THE START
echo $result."<br /><br />\n";
}
}