objective c - Remove HTML Tags from an NSString on the iPhone -


there couple of different ways remove html tags nsstring in cocoa.

one way render string nsattributedstring , grab rendered text.

another way use nsxmldocument's -objectbyapplyingxsltstring method apply xslt transform it.

unfortunately, iphone doesn't support nsattributedstring or nsxmldocument. there many edge cases , malformed html documents me feel comfortable using regex or nsscanner. have solution this?

one suggestion has been opening , closing tag characters, method won't work except trivial cases.

for example these cases (from perl cookbook chapter on same subject) break method:

<img src = "foo.gif" alt = "a > b">  <!-- <a comment> -->  <script>if (a<b && a>c)</script>  <![include cdata [ >>>>>>>>>>>> ]]> 

a quick , "dirty" (removes between < , >) solution, works ios >= 3.2:

-(nsstring *) stringbystrippinghtml {   nsrange r;   nsstring *s = [[self copy] autorelease];   while ((r = [s rangeofstring:@"<[^>]+>" options:nsregularexpressionsearch]).location != nsnotfound)     s = [s stringbyreplacingcharactersinrange:r withstring:@""];   return s; } 

i have declared category os nsstring.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -