简体   繁体   中英

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);

}

use preg_split() to use aa 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM