0

I have this string: <div id="attachment_16696" class="wp-caption alignnone" style="width: 460px">

And I want to have: <div id="attachment_16696" class="wp-caption alignnone" style="width: 275px">

I works almost. The only issue is, when its for example 1000px, because it doesnt match it doesnt work, How can I handle if it is not 460px and I want to have it 260px ?

2
  • why not do, replaceString 460 with 260? Commented Mar 30, 2013 at 18:34
  • because content is variable, it is not anytime 460 Commented Mar 31, 2013 at 11:37

2 Answers 2

3

The following code uses the NSRegularExpression class to replace the width style attribute of a divs HTML markup when supplied in the format you specified:

// Takes Div markup in the following formats:
// <div id="something" class="something" style="width: Xpx">
// <div id="something" class="something" width="X">
// And replaces X with desired value.
- (NSString *)setDivMarkup:(NSString *)markup width:(NSInteger)width
{
    NSString *regexToReplaceWidth = @"width(:|=)(\")?\\s*\\d+\\s*(px)?";

    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexToReplaceWidth
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];

    return [regex stringByReplacingMatchesInString:markup
                                           options:0
                                             range:NSMakeRange(0, markup.length)
                                      withTemplate:[NSString stringWithFormat:@"width$1$2%d$3", width]];
}
Sign up to request clarification or add additional context in comments.

7 Comments

I would suggest using \s* like in mine, rather than a space. But otherwise looks good I think.
@crimson_penguin Makes a good point. Since any number of whitespace between 'width:' and its associated number is still valid markup you may want to replace the regular expression with the following: NSString *regexToReplaceWidth = @"width:\\s*\\d+px";.
Works very good, but there is one more problem: Sometimes the width is styled like width="460". I tried this regex but it doesn´t work: NSString *regexToReplaceWidth = @"width=\"\\d\"";
Well this makes things significantly more complicated. I assume when it is in that format it is its own standalone attribute as opposed to within the 'style' attribute e.g.: <div id="attachment_16696" class="wp-caption alignnone" width="460"> and as such will require the double quotes around the value to remain valid markup?
Yes, so I can't change that ?
|
1

Use a regex: string.replace(/width:\s*\d+px/, 'width: 275px')

EDIT: On second thought, if this is only part of a much bigger string, you probably don't want to replace all widths with 275px (or 260px). You should still use a regex, but I'm not sure exactly what it would be looking for without seeing the context that this is in, and knowing what you do and don't want to replace.

2 Comments

Yeah, this didn't actually specify a language. I was thinking "wait, how is this iPhone related?", but for some reason I assumed it was javascript, because it was operating on HTML.
If you do happen to be using javascript, or someone else is reading this, the usage of the above code is: <code>string</code> is the string you want to run the replacement on, and it'll return the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.