例如我有 html 字符串:
<p>
<img mce_src="http://example.com/apple.png" src="http://example.com/apple.png" width="512" height="512" style="">
<br mce_bogus="1">
</p>
如何更改此属性:width="512"height="512"
例如:width="123"height="123"
?
谢谢
最佳答案
你可以使用
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
您的示例,其中的 html 位于 htmlString
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"width=\"512\""
withString:@"width=\"123\""];
编辑:
使用正则表达式替换(未测试):
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(.*width=\").*?(\".*?height=\").*?(\".*)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:htmlString
options:0
range:NSMakeRange(0, [htmlString length])
withTemplate:@"$1<insert width here>$2<insert height here>$3"];
关于iphone - 更改 NSString 中的 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11952788/