PHP script for searching a web page

Serghei Pogor
1 min readFeb 24, 2023
PHP script for searching a web page

Here’s an example of a PHP script for searching a web page:

PHP

In this script, we define the search term we want to look for in the $searchTerm variable, and the URL of the page we want to search in the $pageUrl variable. We then use the file_get_contents function to get the contents of the page as a string.

Next, we use the strpos function to check if the search term is present in the page contents. The strpos function returns the position of the first occurrence of the search term in the string, or false if the search term is not found.

We use the !== operator to check for a false value specifically, since strpos can also return 0 if the search term is found at the beginning of the string (which would evaluate to false if we used the == operator).

Finally, we output a message indicating whether or not the search term was found on the page.

--

--

Serghei Pogor
Serghei Pogor

Written by Serghei Pogor

Good code is its own best documentation

No responses yet