Jump to content

Finding text within a page


thebestone

Recommended Posts

Hello everyone,

I'm sure this is a pretty easy thing to do but since I'm pretty new at PHP, I'm not sure how to do it.

Basically I want to search a page for words and if they are found I want the script to do one thing. If they're not found, I want it to do something else.

If someone could point me in the right direction it'd be very appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/54056-finding-text-within-a-page/
Share on other sites

Load the contents of the page into a variable (I assume this is already done, but just in case....). Now use either strpos($haystack,$needle[,$offset]) to find it or use preg_match('/regExNeedle/', $haystack [, array &$matches [, PREG_OFFSET_CAPTURE [, int $offset]]]). Sorry for all the syntax, but now you don't have to look it up!

 

Do this:

 

<?php
if (strpos($pageContents,$needle) !== false) {
//Code to execute
} else {
//Default Code
}

//or

if (preg_match($regExNeedle,$pageContents)) {
//Code
} else {
//Default
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.