2

たとえば、私は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"ですか?

ありがとう

4

5 に答える 5

3

あなたが使用することができます

- (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"];

参考: NSRegularExpression

于 2012-08-14T13:03:34.483 に答える
2

正規表現を使用して、RegexKitLiteライブラリを試してください。

NSString *regex = @"(=\"[0-9]+\")";

NSString *replaced = [htmlString stringByReplacingOccurrencesOfRegex:regex usingBlock:^NSString *(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop) {
  return(@"123");
}];
于 2012-08-14T13:34:29.007 に答える
0

imタグ内に新しい幅と高さのプロパティを追加

NSError *regexError = nil;
    NSRegularExpressionOptions options = 0;

    NSString *pattern = @"(img)";
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:options error:&regexError];

    wihoutWidth = [expression stringByReplacingMatchesInString:wihoutWidth
                                                       options:0
                                                         range:NSMakeRange(0,wihoutWidth.length)
                                                  withTemplate:@"$1 width=293 height=150"];
    return wihoutWidth;
于 2014-01-05T18:40:14.537 に答える
0

このコードを使用して幅を置き換える方法は? また、 $1 に文字列または数値を挿入したい

NSError *エラー = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(. width=\"). ?(\". ?height=\")。?(\".*)" オプション:NSRegularExpressionCaseInsensitive エラー:&error];

NSString *modifiedString = [regex stringByReplaceingMatchesInString:htmlString options:0 range:NSMakeRange(0, [htmlString length]) withTemplate:@"$1$2$3"];

于 2016-11-30T00:24:11.733 に答える
0

HTMLを文字列で取得し、forループを使用して関数を作成し、 高さの発生時にその長さと位置を数えてから、これを新しいデータに置き換えます...私はこれをしました...

于 2012-08-14T13:23:58.010 に答える