68素材站

文本编辑器中添加的非网络图片,添加图片前缀URL,方便小程序显示

68素材 7558 0
/// <summary>
        /// 文本编辑器中添加的非网络图片,添加图片前缀URL
        /// </summary>
        /// <param name="strContent"></param>
        /// <returns></returns>
        public string addNativePicUrl(string strContent)
        {
            if (!string.IsNullOrEmpty(strContent))
            {
                Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
                MatchCollection matches = regImg.Matches(strContent);

                int i = 0;

                string[] sUrlList = new string[matches.Count];
                if (matches.Count > 0)
                {
                    // 取得匹配项列表 
                    foreach (Match match in matches)
                    {
                        sUrlList[i++] = match.Groups["imgUrl"].Value;
                    }
                    string strAllEndUrl = strContent;
                    for (int iCount = 0; iCount < sUrlList.Length; iCount++)
                    {
                        string strPicUrl = sUrlList[iCount].ToString();
                        if (!strPicUrl.Contains("http://"))
                        {
                            string strEndUrl = _domain + strPicUrl;//链接
                            string strReplaceText = strAllEndUrl.Replace(strPicUrl, strEndUrl);
                            strAllEndUrl = strReplaceText;
                        }
                    }
                    return strAllEndUrl;
                }
                else
                {
                    return strContent;
                }
            }
            else
            {
                return strContent;
            }
        }


发表评论 (已有0条评论

还木有评论哦,快来抢沙发吧~