
1 cwj14 OP 需求点是这样的,公司需要我用代码做翻译,对接了百度的 api ,我想把符文边编辑器里面的纯文本文字提取出来,翻译好了,再替换回去,PHP 语言,求求各位大佬教教我,替换回去需要保持原有的样式不变 |
2 asLw0P981N0M0TCC 2022 年 1 月 17 日 好像有个 htmlspecial_decode? |
3 shapl 2022 年 1 月 17 日 提取出来很容易。。原封不动替换回去,有难度。 |
4 2i2Re2PLMaDnghL 2022 年 1 月 17 日 正常解析 html ,文本节点投入翻译 API 替换 |
5 jslang 2022 年 1 月 17 日 正则 HTML 替换 textVar.replace(/>([^<]+)</g, ($, text) => {console.log(text);'--------'+text+'-----------'}) |
7 ctro15547 2022 年 1 月 17 日 用 re 写个脚本? |
8 ctro15547 2022 年 1 月 17 日 BS4 应该也可以获取到 txt |
10 imicksoft 2022 年 1 月 17 日 按 html 或 xml 解析,判断 nodeType 是 Text 的节点,把文本内容翻译后再改回去 |
11 jslang 2022 年 1 月 17 日 用这个试试呢,我没有 PHP 环境 preg_split(">([^<]+)<", textVar) 取奇数位索引,翻译好后,合并加上对应的"<"和">" |
12 jslang 2022 年 1 月 17 日 在线模拟了一下,应该可以了 ``` $textVar = '......'; $arr = []; preg_match_all("/>([^<]+)</", $textVar, $arr); var_dump($arr[1]); print_r(preg_split("/>([^<]+)</", $textVar)); ``` |
14 Rache1 2022 年 1 月 17 日 |
15 chengxiao 2022 年 1 月 17 日 你需要学一下 xpath ,然后就是遍历调接口就行了 |
16 RickyC 2022 年 1 月 18 日 ``` function text_from_html($html) { // Remove the HTML tags $html = strip_tags($html); // Convert HTML entities to single characters $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8'); $html_len = mb_strlen($html, 'UTF-8'); // Make the string the desired number of characters // Note that substr is not good as it counts by bytes and not characters $html = mb_substr($html, 0, strlen($html), 'UTF-8'); return $html; } $cOntent= htmlentities(text_from_html($content)); ``` ----- 提取出来可以试试这个。再放回去太难了。 |