2015-09-09 36 views
2

這是我收到的消息列通過表情符號在該JSON:如何從目標C中的JSON中獲取HTML圖像?

<span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/smile.png\" height=\"20\" width=\"20\"></span> <span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/person_with_blond_hair.png\" height=\"20\" width=\"20\"></span> 
+0

看到此鏈接可能是與你的幫助的http:// stackoverflow.com/questions/12496860/ios-strip-img-from-nsstring-a-html-string –

+0

Img Src會經常改變,所以我該如何給它? – Romi

+0

在HTML標記中,圖片url總是像

回答

2
NSString *[email protected]"<span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/smile.png\" height=\"20\" width=\"20\"></span> <span class=\"atwho-inserted\"><img src=\"https://assets-cdn.github.com/images/icons/emoji/person_with_blond_hair.png\" height=\"20\" width=\"20\"></span>"; 

NSError *error = NULL; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?" 
                     options:NSRegularExpressionCaseInsensitive 
                     error:&error]; 

[regex enumerateMatchesInString:htmslStr 
         options:0 
          range:NSMakeRange(0, [htmslStr length]) 
        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { 

         NSString *img = [htmslStr substringWithRange:[result rangeAtIndex:2]]; 

         NSURL *candidateURL = [NSURL URLWithString:img]; 

         if (candidateURL && candidateURL.scheme && candidateURL.host) 
         { 
          NSLog(@"img src %@",img); 
         } 


        }]; 

最終走出看跌期權

enter image description here

+0

finally use this **img** string to where you need –

+0

in **htmslStr** -->開始傳遞你的兒子字符串 –

+0

謝謝@ Anbu.Karthik – Romi