Search code examples
phparraysif-statementoptimizationpreg-match

Optimizing preg_match() in PHP


I'm using the following function in PHP to detect an entity and location from strings that contain "near" in it using preg_match();. Is there a more optimal way to write the code for this? I'm using a lot of if statements and it seems like it could be improved, but I'm not sure how.

// Test cases
$q = "red robin near seattle";
//$q = "red robin near me";
//$q = "red robin nearby";
//$q = "red robin near my location";

function getEntityAndLocation($q){

    $entityAndLocation = array("entity" => null, "location" => null);

    if(preg_match('(nearby)', $q) === 1) {
        $breakdown = explode("nearby", $q);
        $entityAndLocation["entity"] = $breakdown[0];
        $entityAndLocation["location"] = $breakdown[1];
        return $entityAndLocation;
    }

    if(preg_match('(near my location)', $q) === 1) {
        $breakdown = explode("near my location", $q);
        $entityAndLocation["entity"] = $breakdown[0];
        $entityAndLocation["location"] = $breakdown[1];
        return $entityAndLocation;
    }

    if(preg_match('(near me)', $q) === 1) {
        $breakdown = explode("near me", $q);
        $entityAndLocation["entity"] = $breakdown[0];
        $entityAndLocation["location"] = $breakdown[1];
        return $entityAndLocation;
    }

    if(preg_match('(near)', $q) === 1) {
        $breakdown = explode("near", $q);
        $entityAndLocation["entity"] = $breakdown[0];
        $entityAndLocation["location"] = $breakdown[1];
        return $entityAndLocation;
    }

}

if(preg_match('(near)', $q) === 1) {

  $entityAndLocation = getEntityAndLocation($q);

  print_r($entityAndLocation);

}

Solution

  • use preg_split() to use a a regular expression as a delimiter to split a string. You can write a single regexp that matches all your patterns.

    function getEntityAndLocation($q){
    
        $entityAndLocation = array("entity" => null, "location" => null);
    
        $breakdown = preg_split('/near(?:by| my location| me)?/', $q);
        if (count($breakdown) >= 2) {
            $entityAndLocation["entity"] = $breakdown[0];
            $entityAndLocation["location"] = $breakdown[1];
            return $entityAndLocation;
        }
        return $entityAndLocation;
    }
    

    The regular expression matches near, optionally followed by by, my location, or me.