0

I have a string like below -

<html> 
<head> 
</head> 
<body>

<p>***Some Text***.</p>

<p><iframe allowfullscreen="" frameborder="0" height="360" src="//www.some-domain.com/embed/mwOFWJUOpv8" width="600"></iframe></p></body>

I want to replace some of parameters in the iframe tag at the bottom of string as -

<iframe allowfullscreen="" frameborder="0" height="new vlaue" src="//www.some-domain.com/embed/mwOFWJUOpv8" width="new value"></iframe>

I can successfully grab out string starting from "<iframe" to "</iframe>" with the required changes. I am getting problem to replace that whole <iframe ...</iframe> with new string.

So my result should be like this -

<html> 
    <head> 
    </head> 
    <body>

    <p>***Some Text***.</p>

    <p><iframe allowfullscreen="" frameborder="0" height="new vlaue" src="//www.some-domain.com/embed/mwOFWJUOpv8" width="new value"></iframe></p></body>

EDIT : - I have followed this approach, but could not replace whole iframe tag in string.

Please help me out!

2
  • check this answer, similar but same approach can be used stackoverflow.com/questions/16207919/… Commented Jan 19, 2015 at 6:54
  • @JayahariV : can u illustrate EXACT approach? Commented Jan 19, 2015 at 7:14

1 Answer 1

1

Give it a try like this. Suppose str is the grabbed out string mentioned in your post.

str = [str stringByReplacingOccurrencesOfString:@"width=\"600\"" withString:@"width=\"new value\""];

Edit

NSString *theStr= @"<iframe allowfullscreen=\"\" frameborder=\"0\" height=\"360\" src=\"//www.some-domain.com/embed/mwOFWJUOpv8\" width=\"600\"></iframe>";
NSString *replacedStr = [theStr stringByReplacingOccurrencesOfString:@"width=\"600\"" withString:@"width=\"new value\""];

NSRange range1 = [sourceStr rangeOfString:@"<iframe"];
NSRange range2 = [sourceStr rangeOfString:@"iframe>"];

NSRange range3 = NSMakeRange(range1.location, range2.location + range2.length - range1.location);

NSString *finalStr = [sourceStr stringByReplacingCharactersInRange:range3 withString:replacedStr];
Sign up to request clarification or add additional context in comments.

1 Comment

I can grab out substring, and make changes as I want successfully, problem I am getting is to replace that grabbed substring in original string!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.