drr7731 2010-08-16 12:49
浏览 57
已采纳

在php中的html标签之间提取字符串

I want to extract string between html tags and convert it into other language using google api and to append the string with html tags.

For example,

<b>This is an example</b>

I want to extract the string "This is an example" and convert it into other language and then again append the string with bold tag.

Could anyone know how to proceed with this?

Regards Rekha

  • 写回答

3条回答 默认 最新

  • douhuike3199 2010-08-16 13:41
    关注

    Here's a working example of using the Google API to translate server side. The function takes a string as an input and strips HTML tags from it before translating.

    The languages are passed in as arguments.

    <?php
    // This function translates a string $source written in the $fromLang languages to the $toLang language.
    function translateTexts($source, $fromLang, $toLang)
    {
    
        /* Language choices: 'AFRIKAANS' : 'af', 'ALBANIAN' : 'sq', 'AMHARIC' : 'am', 'ARABIC' : 'ar', 'ARMENIAN' : 'hy', 'AZERBAIJANI' : 'az', 'BASQUE' : 'eu', 'BELARUSIAN' : 'be', 'BENGALI' : 'bn', 'BIHARI' : 'bh', 'BRETON' : 'br', 'BULGARIAN' : 'bg', 'BURMESE' : 'my', 'CATALAN' : 'ca', 'CHEROKEE' : 'chr', 'CHINESE' : 'zh', 'CHINESE_SIMPLIFIED' : 'zh-CN', 'CHINESE_TRADITIONAL' : 'zh-TW', 'CORSICAN' : 'co', 'CROATIAN' : 'hr', 'CZECH' : 'cs', 'DANISH' : 'da', 'DHIVEHI' : 'dv', 'DUTCH': 'nl',  'ENGLISH' : 'en', 'ESPERANTO' : 'eo', 'ESTONIAN' : 'et', 'FAROESE' : 'fo', 'FILIPINO' : 'tl', 'FINNISH' : 'fi', 'FRENCH' : 'fr', 'FRISIAN' : 'fy', 'GALICIAN' : 'gl', 'GEORGIAN' : 'ka', 'GERMAN' : 'de', 'GREEK' : 'el', 'GUJARATI' : 'gu', 'HAITIAN_CREOLE' : 'ht', 'HEBREW' : 'iw', 'HINDI' : 'hi', 'HUNGARIAN' : 'hu', 'ICELANDIC' : 'is', 'INDONESIAN' : 'id', 'INUKTITUT' : 'iu', 'IRISH' : 'ga', 'ITALIAN' : 'it', 'JAPANESE' : 'ja', 'JAVANESE' : 'jw', 'KANNADA' : 'kn', 'KAZAKH' : 'kk', 'KHMER' : 'km', 'KOREAN' : 'ko', 'KURDISH': 'ku', 'KYRGYZ': 'ky', 'LAO' : 'lo', 'LATIN' : 'la', 'LATVIAN' : 'lv', 'LITHUANIAN' : 'lt', 'LUXEMBOURGISH' : 'lb', 'MACEDONIAN' : 'mk', 'MALAY' : 'ms', 'MALAYALAM' : 'ml', 'MALTESE' : 'mt', 'MAORI' : 'mi', 'MARATHI' : 'mr', 'MONGOLIAN' : 'mn', 'NEPALI' : 'ne', 'NORWEGIAN' : 'no', 'OCCITAN' : 'oc', 'ORIYA' : 'or', 'PASHTO' : 'ps', 'PERSIAN' : 'fa', 'POLISH' : 'pl', 'PORTUGUESE' : 'pt', 'PORTUGUESE_PORTUGAL' : 'pt-PT', 'PUNJABI' : 'pa', 'QUECHUA' : 'qu', 'ROMANIAN' : 'ro', 'RUSSIAN' : 'ru', 'SANSKRIT' : 'sa', 'SCOTS_GAELIC' : 'gd', 'SERBIAN' : 'sr', 'SINDHI' : 'sd', 'SINHALESE' : 'si', 'SLOVAK' : 'sk', 'SLOVENIAN' : 'sl', 'SPANISH' : 'es', 'SUNDANESE' : 'su', 'SWAHILI' : 'sw', 'SWEDISH' : 'sv', 'SYRIAC' : 'syr', 'TAJIK' : 'tg', 'TAMIL' : 'ta', 'TATAR' : 'tt', 'TELUGU' : 'te', 'THAI' : 'th', 'TIBETAN' : 'bo', 'TONGA' : 'to', 'TURKISH' : 'tr', 'UKRAINIAN' : 'uk', 'URDU' : 'ur', 'UZBEK' : 'uz', 'UIGHUR' : 'ug', 'VIETNAMESE' : 'vi', 'WELSH' : 'cy', 'YIDDISH' : 'yi', 'YORUBA' : 'yo', 'UNKNOWN' : ''  */
    
        // Creating the query URL
        $url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" . urlencode($source) . "&langpair=" . $fromLang . "%7C" . $toLang;
    
        // send translation request
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     
        $response = curl_exec($ch);
        curl_close($ch);
    
        // now, process the JSON string
        $json = json_decode($response, true);
    
        // If response status is okay
        if ($json['responseStatus'] == 200)
        {
            $translated = $json['responseData']['translatedText'];
        } else 
        {
            $translated = "****Error. Couldn't translate.****";
        }     
        // return translated text
        return $translated;
    }
    
    // Get the string you want to translate
    $string = "<b>This is an example</b>";
    // Strip the HTML tags from the strip and translate it.
    echo translateTexts(strip_tags($string), 'en', 'es');
    
    ?>
    

    When you run the code above, you should add in a proper self identification to the header.

    References:

    Google Translation API section for Flash and Non-Javascript Interfaces
    PHP cUrl examples
    json_decode()
    strip_tags()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?