I have a string I want to display inside the uiwebview that may look like this:
"blah blah blah [IMG]you.png[/IMG] blah blah [IMG]me.png[/IMG]"
I want it replaced like this:
"blah blah blah <img src=\"you.png\"> blah blah <img src=\"me.png\">"
I can already get the string between the [IMG]…[/IMG] the problem is that it can only get the first set of [IMG]…[/IMG] and the rest of the occurrence cannot.
NSRange startRange = [finalQuestionString rangeOfString:@"[IMG]"];
if (startRange.location != NSNotFound) {
NSRange targetRange;
targetRange.location = startRange.location + startRange.length;
targetRange.length = [finalQuestionString length] - targetRange.location;
NSRange endRange = [finalQuestionString rangeOfString:@"[/IMG]" options:0
range:targetRange];
if (endRange.location != NSNotFound) {
targetRange.length = endRange.location - targetRange.location;
NSString *imageFile = [finalQuestionString
substringWithRange:targetRange];//the extracted filename
}
}
so how can I get all the occurrences of [IMG]…[/IMG] and replace it with
"<img src=\"file.png\">"
any help would be appreciated. Thanks!
Since
[IMG]will always become<img src=\"and[/IMG]will always become\">", why not do something like this:Memory management is left as an exercise for the reader 🙂