iOS html5文本解析

在某些场景会遇到需要将网页文本解析为字符串并保留格式的问题,下面列举OC/swift的解析方法



OC

缺少P标签的解析,参考swift

-(NSString*)html5CodeToString {

    NSString *str = [self stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];

    str = [strstringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];

    str = [strstringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];

    str = [strstringByReplacingOccurrencesOfString:@"&quot;" withString:@"“"];

    str = [strstringByReplacingOccurrencesOfString:@"&reg;" withString:@"®"];

    str = [strstringByReplacingOccurrencesOfString:@"&copy;" withString:@"©"];

    str = [strstringByReplacingOccurrencesOfString:@"&trade;" withString:@"™"];

    str = [strstringByReplacingOccurrencesOfString:@"&ensp;" withString:@" "];

    str = [strstringByReplacingOccurrencesOfString:@"&emsp;" withString:@" "];

    str = [strstringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];

    returnstr;

}

swift

extension String {

    funchtml5CodeToString() ->String{

        varstr =self.replacingOccurrences(of:"&lt;", with:"<")

        str = str.replacingOccurrences(of:"&gt;", with:">")

        str = str.replacingOccurrences(of:"&amp;", with:"&")

        str = str.replacingOccurrences(of:"&quot;", with:"“<”")

        str = str.replacingOccurrences(of:"&reg", with:"©")

        str = str.replacingOccurrences(of:"&copy;", with:"©")

        str = str.replacingOccurrences(of:"&trade;", with:"™")

        str = str.replacingOccurrences(of:"&ensp;", with:" ")

        str = str.replacingOccurrences(of:"&emsp;", with:" ")

        str = str.replacingOccurrences(of:"&nbsp;", with:" ")

        str = str.replacingOccurrences(of:"<p>", with:"")

        str = str.replacingOccurrences(of:"</p>", with:"\n")

        returnstr

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容