Return text between line breaks from html page

Discussion in 'iOS Programming' started by newtoiphonesdk, Apr 13, 2012.

  1. newtoiphonesdk macrumors 6502a

    Jul 30, 2010
    I use:
    NSURL *url = [NSURL URLWithString:@""];
        self.content = [NSString stringWithContentsOfURL:url];
    This returns all of the text, and pretty much everything in it. If the user has stored their name in the app, it scans the text in self.content for a match. If it has a match, it gives the user a message in a uialertview. I would like to provide more details about why their name is in the document, but am unsure about how to do this. As I control the document, I can format it any way I like. I was thinking about having only 1 name per line, and having the alertview show the entire line. How could I go about scanning for a match with the NSUserDefaults for their name, and return everything else on that same line between the line breaks?
  2. cMacSW macrumors regular

    Mar 20, 2006
    Not exactly sure from your description, but I'd approach it this way:

    Use NString's componentsSeparatedByString method and pass @"\n" for the separator. This will give you an array with one line of text in each element of the array.

    Than I'd use NSArray's filteredArrayUsingPredicate method to determine if any lines contain the name.

    This would be good for searching for multiple items, but maybe not efficient if your looking for just 1 name once. If that's the case I'd search the string directly and extract the data directly.
  3. newtoiphonesdk, Apr 13, 2012
    Last edited: Apr 13, 2012

    newtoiphonesdk thread starter macrumors 6502a

    Jul 30, 2010
    There are some instances in which the name could be entered in multiple times. It is basically just a schedule html file, and I currently have it string the contents. If it finds a match with the NSUserDefault for their name, it pops up the alert view stating they have been scheduled for something, and need to check the schedule to see what. I would like to be able to eliminate that last step, and have the alertview pull from the string, telling them exactly what they are doing. Just not sure how to extract that. I do get what you were saying at the beginning, just not the last part of "If that's the case..."

    Edit, and I know that stringContentsofUrl is deprecated, so in future versions not sure of how to do even the current part I am doing.
  4. cMacSW macrumors regular

    Mar 20, 2006
    Can you provide an example of the data maybe a line or two and the field you're seating for and the field you'd like to extract.
  5. newtoiphonesdk thread starter macrumors 6502a

    Jul 30, 2010
    The html is

    Each line has an area of worship, and the person assigned to that area. I'd like to search for a name, and then what line it is on, and present an UIAlertView that combines both lines to tell the user exactly what they are scheduled for.
  6. cMacSW macrumors regular

    Mar 20, 2006
    So here's a random line from your sample data:

    <p class="p1"><span class="s1">Welcome</span>: Tyler Brassfield</p>

    Assuming you use NSStrings componentsSeparatedByString then each component would be like the line above. you could then use the same method to break this down into the following components
    <p class="p1">
    <span class="s1">
    : Tyler Brassfield</p>

    then you can use substring methods to trim the strings.

Share This Page