1

Web アプリケーションで Google 方向 API を使用しています。Google が提供する方向説明を短くする方法はありますか?

たとえば、

Take the 2nd right.
Take the 2nd left toward ...

短くしてもいいですか?長過ぎます。

私はこのようにそれを作ることができます:

2nd right>2nd left>

結果を変更する方法はありますか?Web アプリケーションの開発に PHP を使用し、API の結果を表示するために JSON 形式を使用しています。編集: API の結果が正しく表示されます。しかし、「Take」、「The」、「at」などの特定の一般的な単語を削除したいのですが、コードの一部を表示する API 結果:if ($data->status === 'OK') { $route = $data->routes[0]; foreach ($route->legs as $leg) { foreach ($leg->steps as $step) { echo $step->html_instructions . "<br>\n";

4

1 に答える 1

2

これは私にとってはうまくいきました。うまくいけば、あなたにもうまくいくでしょう...

<?php
$test1 = 'Take the 2nd right.';
$test2 = 'Take the 2nd left toward the exit, then...';
$reg_find = '/Take the (.*?) (right|left).*/';
$reg_replace = '$1 $2';
$results = array(
    preg_replace($reg_find, $reg_replace, $test1),
    preg_replace($reg_find, $reg_replace, $test2)
);
echo implode('>', $results);
?>

編集:

より柔軟なソリューションのために、私はこれを作成しました:

<?php
$test1 = 'Take the 2nd right.';
$test2 = 'Take the 2nd left toward the exit, then ...';
$remove = '/( ?)(take|then|at|toward|the|exit|\.|,)( ?)/i';
$results = array(
    preg_replace($remove, '', $test1),
    preg_replace($remove, '', $test2)
);
echo implode('>', $results);
?>
于 2012-08-06T05:07:53.677 に答える