手機與網頁溝通XML傳遞!

XML遇到 "<",">","&",就掛掉了,沒辦法拆XML標籤

PHP

/******************************
名稱說明:取代特殊字元
功能說明:取代 & , > , <
參數說明:$STR
回傳說明:$STR
製作人員:Nick
製作日期:2012/03/15
更新日期:
更新說明:
******************************/

    function replacespecialstr($str)
    {
        $str = str_replace("&","!AMP;",$str);
        $str = str_replace("<","!LT;",$str);
        $str = str_replace(">","!GT;",$str);
        return $str;
    }


Xcode

- (NSString *)ReplaceSpecialStr:(NSString *)SPECIALSTR
{
    SPECIALSTR = [SPECIALSTR stringByReplacingOccurrencesOfString:@"!AMP;" withString:@"&"];
    SPECIALSTR = [SPECIALSTR stringByReplacingOccurrencesOfString:@"!LT;" withString:@"<"];
    SPECIALSTR = [SPECIALSTR stringByReplacingOccurrencesOfString:@"!GT;" withString:@">"];
    return SPECIALSTR;
}

//--------------------------------------------------UTF8的編碼中文--------------------------------------------------
//md5加密
- (NSString *) md5:(NSString *)str
{
    if( nil == str ){return nil;}
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH]; //長度16
    CC_MD5( cStr, strlen(cStr), result ); //加密方法
    return [[NSString stringWithFormat:
             @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ]lowercaseString];
}



//UTF8的編碼中文
-(NSString *)StringEnccode:(NSString *)str
{
    NSString *encodestr=(__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge_retained CFStringRef)str, NULL, CFSTR(";,/?:@&=+$#"), kCFStringEncodingUTF8);
    return encodestr;
}

留言

熱門文章