這裏主要是以p標籤和img標籤進行分割,如果是以<br />標籤換行的話可以先將<br />標籤替換爲<p>標籤再分割,話不多說,上代碼
一,以p標籤進行分段
public static JSONArray getContentJson4Part(String articleText) {
JSONArray array = new JSONArray();
//根據<P>來分段
if (StringUtils.isNotBlank(articleText)) {
StringBuffer currentSeq = new StringBuffer("1");
int textLength = articleText.length();
int curLength = 0;
while (curLength < textLength) {
int PPosStart = articleText.indexOf("<p", curLength);
if (PPosStart < 0) {//有P標籤
PPosStart = articleText.indexOf("<P", curLength);
}
if (PPosStart >= 0) {//有P標籤
int PPosEnd = articleText.indexOf("</p>", PPosStart);
if (PPosEnd < 0) {
PPosEnd = articleText.indexOf("</P>", PPosStart);
}
if (PPosEnd != -1) {
if (curLength == PPosStart) {
PPosEnd = PPosEnd + 4;
String text = articleText.substring(PPosStart, PPosEnd);
getImagJson(text, array, currentSeq);
curLength = PPosEnd;
} else {
String text = articleText.substring(curLength, PPosStart);
if (!StringUtils.isBlank(text)) {
getImagJson(text, array, currentSeq);
}
curLength = PPosStart;
}
} else {//沒有以</p>結束P標籤
int PPosTag = articleText.indexOf("<p", PPosStart+1);
if (PPosTag < 0) {
PPosTag = articleText.indexOf("<P", PPosStart+1);
}
if (PPosTag != -1) {
String text = articleText.substring(PPosStart, PPosTag);
if (!StringUtils.isBlank(text)) {
getImagJson(text, array, currentSeq);
}
curLength = PPosTag;
} else {
String text = articleText.substring(PPosStart, textLength);
if (!StringUtils.isBlank(text)) {
getImagJson(text, array, currentSeq);
}
curLength = textLength;
}
}
} else {
String text = articleText.substring(curLength, textLength);
if (!StringUtils.isBlank(text)) {
getImagJson(text, array, currentSeq);
}
curLength = textLength;
}
}
}
return array;
}
二 ,分割文字和圖片
public static void getImagJson(String text, JSONArray array, StringBuffer currentSeq) {
if (!StringUtils.isBlank(text)) {
int start = 0;
int length = text.length();
while (start < length) {
int imgPosStart = text.indexOf("<img", start);
if (imgPosStart < 0) {
imgPosStart = text.indexOf("<IMG", start);
}
if (imgPosStart >= 0) {
int imgPosEnd = text.indexOf('>', imgPosStart);
if (imgPosEnd > imgPosStart) {
imgPosEnd++;
String imgUrl = text.substring(imgPosStart, imgPosEnd)
.replaceFirst(".*src=\"(.+?)\".*", "$1");
String tbUrl = imgUrl;
if (imgPosStart == start) {
if (!StringUtils.isBlank(tbUrl)) {
JSONObject obj = getItem("image", tbUrl, currentSeq);
if (obj != null) {
array.add(obj);
}
}
} else {
JSONObject obj = getItem("text",text.substring(start, imgPosStart), currentSeq);
if (obj != null) {
array.add(obj);
}
if (!StringUtils.isBlank(tbUrl)) {
JSONObject imgObj = getItem("image", tbUrl, currentSeq);
if (imgObj != null) {
array.add(imgObj);
}
}
}
start = imgPosEnd;
} else {
JSONObject obj = getItem("text",text.substring(start, imgPosStart), currentSeq);
if (obj != null) {
array.add(obj);
}
start = imgPosStart + 1;
}
} else {
JSONObject obj = getItem("text", text.substring(start, length), currentSeq);
if (obj != null) {
array.add(obj);
}
start = length;
}
}
}
}
三,處理分割後的結果
public static JSONObject getItem(String type, String value, StringBuffer currentSeq) {
JSONObject obj = null;
value = value.replaceAll(" ", " ");
if (!StringUtils.isBlank(type) && !StringUtils.isBlank(value)) {
if ("text".equals(type)) {
if(value.startsWith("<p><embed") || value.startsWith("<p><tableflag_")){
value = value.replaceAll("<p>", "").replaceAll("</p>", "");
}else {
if(value.indexOf("u-arr-u")!=-1){ //如果是圖文描述
type = "desc";
}
value = value.replaceAll("<[^<^>]*?>", "").replaceAll(
"</[^<^>]*?>", "").replaceAll("((http|https)://|www\\.)[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?", "");
}
}
if (!StringUtils.isBlank(value) && !" ".equals(value)) {
obj = new JSONObject();
obj.put("id",currentSeq.toString());
int temp = Integer.parseInt(currentSeq.toString())+1;
currentSeq.setLength(0);
currentSeq.append(temp);
obj.put("type", type);
obj.put("value",T.toHtml(value.replace(" ", "")));
}
}
return obj;
}
四 ,結果示例:
{
"data": [
{
"content": [
{
"id": "1",
"type": "text",
"value": "眼部是心靈的窗口,又提上了這個俗套話,話雖俗,但是事在理,無論是護膚上還是彩妝上,都無法忽視眼部的存在感。"
},
{
"id": "2",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817062_1384931193469_1024x1024.jpg"
},
{
"id": "3",
"type": "text",
"value": "所以最重要的就是提前的預防,後悔沒有早一點的重視,所以眼部還是會有微微的細紋,如果正在看貼的你還年輕,那麼趁早的使用眼霜吧,等到細 紋出現就要追悔莫及咯,好了,入正題,今天帶來的是Prox純煥方程式特護修紋眼霜,顧名思義就能知道這款眼霜的主要功效啦,修護細紋,正式蕊蕊所需要的 啦"
},
{
"id": "4",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817061_1384931192102_1024x1024.jpg"
},
{
"id": "5",
"type": "text",
"value": "來自Pro-X by Olay的專業級產品,從包裝上看起來就超級的有科技感哦,紅色的一席,讓這個寒冷的秋冬多了絲絲暖意有木有?~"
},
{
"id": "6",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817058_1384931188971_1024x1024.jpg"
},
{
"id": "7",
"type": "text",
"value": "瓶口是精緻的壓嘴狀,取用起來非常方便,這是蕊蕊大愛的方式,既能方便使用,更可以有效的防止外部以及手部對內在眼霜的污染"
},
{
"id": "8",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817057_1384931187308_1024x1024.jpg"
},
{
"id": "9",
"type": "text",
"value": "Pro-X by Olay是以皮膚基因組科學爲研發基礎,所以相對於市面上的普通護膚品要更爲專業,在選擇護膚品上,蕊蕊不主張以貴爲好,以貴爲美,重要的還是要適合自己"
},
{
"id": "10",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817060_1384931190525_1024x1024.jpg"
},
{
"id": "11",
"type": "text",
"value": "【優點】這一款是作爲修護眼部肌膚使用,雖然是修護,但是沒有細紋的使用會起到預防的作用,早晚使用,不要偷懶,這樣才能達到理想的效果,使用眼霜可以有效的改善皮膚屏障,促進皮膚更新。而且很溫和無刺激,質地較清爽,眼部肌膚使用之後無負擔。"
},
{
"id": "12",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817102_1384931252006_1024x1024.jpg"
},
{
"id": "13",
"type": "text",
"value": "可以看到,它的PH值非常溫和的,無刺激,適合大多數MM的肌膚使用"
},
{
"id": "14",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817413_1384931543769_1024x1024.jpg"
},
{
"id": "15",
"type": "text",
"value": "【缺點】挺好的一款眼霜,暫時沒有太大的缺點。【滋潤度】延展性還不錯,沒有乾涸的感覺,輕鬆一下便可以完全的延展開,指尖滑潤, 感覺到眼霜的質地非常的輕盈,尤其是在秋冬這樣乾燥的季節,這樣的質地會相對更加的保溼,完全吸收後,肌膚清爽舒適,使用下來沒有一絲的油膩感。測試了下 水分效果,比使用前增加了不少,超級補水的說,相對於一般的眼霜來說,這樣的補水效果絕對值得稱讚哦"
},
{
"id": "16",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817101_1384931250871_1024x1024.jpg"
},
{
"id": "17",
"type": "image",
"value": "https://img.pconline.com.cn/images/upload/upc/tx/ladybbs6/1311/20/c0/28817100_1384931249775_1024x1024.jpg"
},
{
"id": "18",
"type": "text",
"value": "可以很清楚地看到使用前跟使用後的水分數據區別,從33.3%提升到51.5%"
}
],
"createAt": "2013-11-25",
}
],
"msg": "請求數據成功",
"status": 0
}