Change Preposition according to State

1

I have the following situation:

I'm using an IP-based location API link

Everything is working perfectly. What I can not do is change the following:

Hello visitor "from" São Paulo Hello Visitor "of" Rio de Janeiro

Can you change this preposition automatically according to status?

    
asked by anonymous 01.08.2016 / 19:39

2 answers

4

Though archaic, it would work with array:

<?php
$uf = "São Paulo";
$preposicoes = array(
    "Rio de Janeiro" => "do",
    "São Paulo" => "de"); // complete com os demais estados

echo "Olá visitante " . $preposicoes[$uf] . " " . $uf . "!";
?>
    
01.08.2016 / 19:47
0

An alternative via json worked quietly here:

            $(function(){
                $.ajax({
                    url: "http://ip-api.com/json/208.80.152.201",
                    context: document.body
                }).done(function(response) {
                    console.log(response);
                    $("#local-visitante").html(response.city);
                });
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><p>Olávisitantede<spanid="local-visitante"></span></p>

Via PHP also worked ok by following the documentation specifications :

<?php
    $ip = "177.33.22.36"; // the IP address to query
    $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
    echo 'Olá visitante de: ' . $query['city'];
?>

I hope I have helped.

    
01.08.2016 / 19:52