user4189129
user4189129

Reputation:

Checking if a string has certain characters in it in PHP

I'm using an API which retrieves information of companies in France and outputs custom JSON of this information. You put in a company name and it returns all the companies' information that match the search word. This system isn't 100% perfect though, since it also returns a lot of companies that almost match the search value.

For example, I search for 'abc' and in the return I also get companies with the name value 'abl'.

So I wanted to filter these out before putting them in a result array.

public function transform($obj){

//The $obj is the information retrieved from the API in JSON.
    $data = json_decode($obj, true);
    $retval = array();

//The '$name = $data["params"]["name"];' is the name the API used as search parameter. 
    $name = $data["params"]["name"];

    foreach ($data["companies"] as $item){

//The '$item["names"]["best"]' is the name of the company.
        if(strpos($item["names"]["best"], $name) !== false){
            $retval[] = [
            "Company name" => $item["names"]["best"],
            "More info" => array(
                "Denomination" => $item["names"]["denomination"],
                "Commercial name" => $item["names"]["commercial_name"]
            ),
            "Siren number" => $item["siren"],
            "Street" => $item["address"],
            "Zip code" => $item["postal_code"],
            "City" => $item["city"],
            "Vat_number" => $item["vat_number"],
            "Established on" => $item["established_on"]
            ];
        }            
    }
    return json_encode($retval, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}

But even when I compare the strings before I create the array object, it still returns these 'wrong' companies. Anyone has an idea on what I'm doing wrong? Any help would be greatly appreciated!

Edit: In case anyone wants to know which API I'm using, here's the link: https://firmapi.com/

Upvotes: 0

Views: 57

Answers (1)

Farhan
Farhan

Reputation: 1483

Use preg_match() instead of strpos to find exact match. Hope this will help you

public function transform($obj){

//The $obj is the information retrieved from the API in JSON.
    $data = json_decode($obj, true);
    $retval = array();

//The '$name = $data["params"]["name"];' is the name the API used as search parameter. 
    $name = $data["params"]["name"];

    foreach ($data["companies"] as $item){

//The '$item["names"]["best"]' is the name of the company.
        $string = 'Maramures';
        if ( preg_match("~\b$string\b~",$name) )
            $retval[] = [
            "Company name" => $item["names"]["best"],
            "More info" => array(
                "Denomination" => $item["names"]["denomination"],
                "Commercial name" => $item["names"]["commercial_name"]
            ),
            "Siren number" => $item["siren"],
            "Street" => $item["address"],
            "Zip code" => $item["postal_code"],
            "City" => $item["city"],
            "Vat_number" => $item["vat_number"],
            "Established on" => $item["established_on"]
            ];
        }            
    }
    return json_encode($retval, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}

Upvotes: 1

Related Questions