preg_match_all help needed!

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!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

preg_match_all help needed!

Post by kaisellgren »

Code: Select all

$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!";

Please help! :?:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

This might do the trick (not tested):

Code: Select all

<?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.

*/

?>
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Thanks for helping me out, but luckily I got it working with regular expressions!!! :D

First time doing anything like this:

Code: Select all

$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";
   }
 }
What do you think? It seems to work very well!
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Well done! Better than my shoddy attempt :D
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Hehe yes, I'm glad I got it working on my own with php doc :D
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

That's the way to do it
Post Reply