Google Translate API v2 - skip words and special character

醉酒当歌 提交于 2019-12-25 05:04:18

问题


I'm trying to use the Google API V2 with PHP

I need to exclude some words so I'm wrapping them into <span class"notranslate">WORD</span>

The problem is that my text contains some special chars so I'm using urlencode($input)

The issue is that urlencode breaks the exclude word functionality ...

What I'm doing wrong ?

Example

$url = "https://www.googleapis.com/language/translate/v2";
$params = array(
    'key' => $helper->getConfigValue('google_api'),
    'source' => $from,
    'target' => $to, // NOTE for CHINESE zh-CN
    'q' => urlencode($input),
);
$url .= '?' . http_build_query($params);

$handle = curl_init($url);
....

回答1:


http_build_query already applies url encoding, so the urlencode($input) is redundant

<?php
$str = '<span class="notranslate">WORD</span>';

$params = [
    'single' => $str,
    'double' => urlencode($str)
];           
echo http_build_query($params);

Results in:

single=%3Cspan+class%3D%22notranslate%22%3EWORD%3C%2Fspan%3E
double=%253Cspan%2Bclass%253D%2522notranslate%2522%253EWORD%253C%252Fspan%253E

Note the double encoding on double.



来源:https://stackoverflow.com/questions/37690723/google-translate-api-v2-skip-words-and-special-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!