views:

53

answers:

2

hi, i think it is a simple question but i dont know how to solve it.

i have a NSString that containes html content.

i want to extract some tags.

NSString *string=@"test some text <img src='http://www.xyz.com/a.jpg' > blah blah <a href='asdfg'>aaaa</a> bbbb cccc";

i want to take img & "a" tag into new string. then i will display it in UIWebView.

how can i parse it?

+1  A: 

The best way is to write a partial tag parser. This is not very difficult.

Another option is to simply find the position of src=' and then the position of the closing "`" and then take the string in between.

You can do this with NSString's rangeOfString: methods.

St3fan
+1  A: 

You can use Regular Expressions, I have used this library to scrap web pages in the iPhone http://regexkit.sourceforge.net/RegexKitLite/index.html

GuidoMB
+1 regexkitlite is a godsend. I don't know why there isn't a native regex function in objective-c
Thomas Clayson
Don't use RegEx to parse HTML: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
Jasarien
@Thomas Clayson - there is: NSRegularExpression is in 4.0.
Jasarien
@Jasarien... really? thats good - I'll look into that. :)
Thomas Clayson