admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.yeshi.fanli.util.goods;
 
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import org.jsoup.Jsoup;
 
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 商品文案
 */
public class GoodsTextUtil {
 
 
    /**
     * APP内推荐语编码
     *
     * @param content
     * @param linkIconPath
     * @param linkName
     * @return
     */
    public static String encodeAppHtmlText(String content, String linkIconPath, String linkName) {
        //将链接替换成客户端可展示的内容
        //提取链接
        String result = content;
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(content);
        while (matcher.find()) {
            String url = matcher.group();
            if (!StringUtil.isNullOrEmpty(url)) {
                //将链接替换成APP输出的标准模式
                result = result.replace(url, String.format("<img src='%s'><a href=\"%s\">%s</a>", linkIconPath, url, linkName));
            }
        }
        //提取淘口令;
        List<String> taoBaoTokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(result);
        if (taoBaoTokenList != null)
            for (String token : taoBaoTokenList) {
                //替换淘口令
                result = result.replace(token, String.format("<img src='%s'><a href=\"%s\">%s</a>", linkIconPath, token, linkName));
            }
        //替换换行符
        result = result.replace("\n", "<br>");
        return result;
    }
 
    /**
     * APP内推荐语反编码
     *
     * @param decodeContent
     * @return
     */
    public static String decodeAppHtmlText(String decodeContent) {
        //替换img标签
        String result = decodeContent.replaceAll("<img[^>]*>", "");
        //替换a标签
        Pattern p = Pattern.compile("<a[^>]*>([^<]*)</a>");
        Matcher m = p.matcher(result);
        while (m.find()) {
            String r = m.group(0);
            String href = Jsoup.parse(r).getElementsByTag("a").get(0).attr("href");
            result = result.replace(r, href);
        }
        //替换换行符
        result = result.replace("<br>", "\n");
        return result;
    }
 
 
}