Page 1 of 1

eregi with regular expressions help please...!!

Posted: Fri Apr 02, 2004 7:42 am
by idotcom
Hi guys and girls :roll:

I have spent a few hours trying to get the result I need, but no luck, or at least for exactly what I need. I am trying to use eregi() to match a number of keywords against $words in exploded array.

Confused? Let me explain more.

The reason I need to do this is because I am using it to strip words out of text result until one or more keywords are found. I do this to show a max 50 words preview of a pages content in search results, but to start content "preview" where it finds the keyword(s).

Example...

say this is the paragraph in a page:

It could be said that, but for an obscure thirteenth century manuscript,
The Lord of the Rings might never have seen publication. Most people who
have expressed something more than a passing fancy sooner or later hear
that Tolkien actually pulled The Lord of the Rings from consideration by
its eventual published, George Allen & Unwin, and submitted the work
instead to Milton Waldman at Collins.


now say the keywords are: thirteenth and Rings

the preview would be:

thirteenth century manuscript,
The Lord of the Rings might never have seen publication. Most people who
have expressed something more than a passing fancy sooner or later hear
that Tolkien actually pulled The Lord of the Rings from consideration by
its eventual published, George Allen & Unwin, and submitted the work
instead to Milton Waldman at Collins.

OR

Rings might never have seen publication. Most people who
have expressed something more than a passing fancy sooner or later hear
that Tolkien actually pulled The Lord of the Rings from consideration by
its eventual published, George Allen & Unwin, and submitted the work
instead to Milton Waldman at Collins.

Which ever keyword it finds first is the goal.

Here is what I have now

Code: Select all

$words = explode(" ", $result_page_text);

for ($t = 0; $t < count($words); $t++) 
{

  if (!eregi("$search_keys[0]", "$words[$t]"))
 
               {
               str_replace("", $words[$t], $result_page_text);
                } 
else 
               {
    	$truned_text = implode(" ", array_slice($words, $t));
                }
}

Now, this works, but only using the first keyword


if I do this

Code: Select all

if (!eregi("($search_keys[0]|$search_keys[0])", "$words[$t]"))
It tends to grab the second keyword first.

So I hope your following me here...! :wink:

I need the check multiple $search_keys against the $words.

I am assuming I need to do this with regular expressions which I really dont know much about. I have been trying to learn though, its just for the begining, these expressions look like crazy people typing errors!!

If you have any ideas or preferably code samples, that would be GREAT!

Thanks in advance,

Chris

Posted: Fri Apr 02, 2004 8:01 am
by Pozor
hello,

Only a little note:

ereg and stuff is old, you should use preg and frieds :wink:

they are faster and more powerful.

greez Pozor

Posted: Fri Apr 02, 2004 8:26 am
by twigletmac
What does the $search_keys array look like? You may not have to use regular expressions for this, it could possibly be made a bit simpler.

Mac

Posted: Fri Apr 02, 2004 10:01 pm
by idotcom
twigletmac wrote:What does the $search_keys array look like? You may not have to use regular expressions for this, it could possibly be made a bit simpler.

Mac
Really it does not matter to me how its done, so long as it works. Speed would be nice too.

I just explode the $search_string into

$search_keys[0]
$search_keys[1]
$search_keys[2]
$search_keys[3]

and so on... It would be nice to automatically use any or all the $search_keys based on how many there are. Instead of me predefining them, since if I set the script to use say 3 search_keys and there are only 2 then the script results an error.

If anyone has a better way for me to do this, PLEASE if you would, provide some code.

Just to make sure and keep it simple, here is what I need to do.

Take the search string and compare all the keywords against the content (text) from the database and strip words out of the text until it finds or mathces any of the search_key words.

Thanks for your replies so far guys! I know some of you smart guys will have a solution, since I'm obviously not that great with php yet!

Chris