PHP - using strpos() to find any of a number of words within an HTML page

614 views Asked by At

I've looked for numerous examples on StackOverflow so that this question would not be redundant. I haven't seen an answer that has worked for my situation. Thanks in advance!!

I created an auto response script used to respond to incoming leads for a construction business. The inbound lead data is listed in within an HTML document (internal web page). Because off various agreements with other companies, there are some cities that they cannot bid on jobs for in our area.

This is the code I've used to successfully check for the instance of one city in the $page ($page being the html page):

<?php 
$flag == false;
         //checks for cities we do not serve//
if(strpos($page, 'Lakeland') !== false){
$flag == true;
}

if($price != 0 && $flag == false){
        // SENDS QUOTE 

if(strpos($page, '"success":true')){
    echo "Status: SENT QUOTE" . PHP_EOL;
?>

What I really need to do is have it check for more than just one city (e.g. Lakeland or Zephryhills or Tarpon Springs, etc) if ANY of these cities are listed on the page, $flag == true; and stops submitting the quote. I've tried the following with no avail....with this code it inputs the message but will not submit an auto-response to any leads:

<?php
$flag == false;

if (strpos($page, 'Lakeland') !== false || strpos($page, 'Zephryhills') !== false || strpos($page, 'Tarpon Springs') !== false){
$flag == true;

if($price != 0 && $flag == false){
    // SENDS QUOTE 

if(strpos($page, '"success":true')){
   echo "Status: SENT QUOTE" . PHP_EOL;
?>

@Vishwa So, an array like this, followed by your code?

$cities = array('Lakeland', 'Zephyrhills', 'Tarpon Springs');
1

There are 1 answers

2
Vishwa On

If you can put that list of cities in an array (I assume, you could) , this function might help you.

function arraypos($html , $cities)
{
    foreach ($cities as $city) {
        if (strpos($html, $city) !== FALSE)   return true;
    }
    return false;
}

Now, you can just use

   $flag == arraypos($page, $array_of_cities);