iphone URL Parser , iphone Web Link Parser
Posted on Monday, April 18, 2011
|
No Comments
NSMutableArray *myarray;// = [[NSMutableArray alloc]init];
myarray = [self linkParser:[textView text]];
NSString *temp = [myarray objectAtIndex:0];
here in temp variable containt a url link and the start position and end position of this link.
// this is the link parser
-(NSMutableArray*) linkParser : (NSString *) inputText {
NSMutableArray *returnUrlArray = [[NSMutableArray alloc]init];
NSString *returnURL = [[NSString alloc]initWithString:@""];
NSString *startPos =[[NSString alloc]initWithString:@""];;
NSString *endPos = [[NSString alloc]initWithString:@""];;
// flag settings
BOOL HTTP_FLAG = FALSE;
BOOL WWW_FLAG = FALSE;
BOOL matched_FLAG =FALSE;
for (int i=0; i< [inputText length]; i++) {
char c;
c = [inputText characterAtIndex:i];
// settings which type of flag checking
// settings flag for http:
if(HTTP_FLAG == FALSE && WWW_FLAG == FALSE)
{
if(c == 'H' || c == 'h')
{
HTTP_FLAG = TRUE;
matched_FLAG =FALSE;
}
}
// settings flag for www.
if(WWW_FLAG == FALSE)
{
if(c == 'W' || c == 'w')
{
WWW_FLAG = TRUE;
matched_FLAG =FALSE;
}
}
//-----------------************-----------------
// if HTTP FLAG TRUE THEN TRY TO PARSE NEXT CHARATER LIKE ttp:
if (HTTP_FLAG) {
NSString *checkString = [[NSString alloc]initWithString:@""];
if (matched_FLAG==FALSE)
{
for (int j=0; j<=6; j++) {
if(i+j <= [inputText length]) {
NSString *temp;
temp = [NSString stringWithFormat:@"%c",[inputText characterAtIndex:i+j]];
checkString = [NSString stringWithFormat:@"%@%@",checkString,temp];
}
}
if ([checkString isEqualToString:@"http://"] || [checkString isEqualToString:@"HTTP://"])
{
matched_FLAG = TRUE;
NSString *temp;
temp = [NSString stringWithFormat:@"%c",c];
startPos = [NSString stringWithFormat:@"%i",i];
returnURL = [NSString stringWithFormat:@"%@%@",returnURL,temp];
} else {
HTTP_FLAG = FALSE;
}
}
else if (matched_FLAG) {
if (c != ' ' && c != '\n') {
NSString *temp;
temp = [NSString stringWithFormat:@"%c",c];
returnURL = [NSString stringWithFormat:@"%@%@",returnURL,temp];
if (i+1 == [inputText length]) {
matched_FLAG = FALSE;
HTTP_FLAG = FALSE;
endPos = [NSString stringWithFormat:@"%i",i-1];
returnURL = [NSString stringWithFormat:@"%@ ,%@ ,%@",returnURL,startPos,endPos];
[returnUrlArray addObject:returnURL];
returnURL = @"";
}
} else if (c == ' ' || c == '\n') {
matched_FLAG = FALSE;
HTTP_FLAG = FALSE;
endPos = [NSString stringWithFormat:@"%i",i-1];
returnURL = [NSString stringWithFormat:@"%@ ,%@ ,%@",returnURL,startPos,endPos];
[returnUrlArray addObject:returnURL];
returnURL = @"";
}
}
} // end HTTP_FLAG
//-----------------************-----------------
// if HTTP FLAG TRUE THEN TRY TO PARSE NEXT CHARATER LIKE ttp:
if (WWW_FLAG) {
NSString *checkString = [[NSString alloc]initWithString:@""];
if (matched_FLAG==FALSE)
{
for (int j=0; j<=3; j++) {
if(i+j <= [inputText length]) {
NSString *temp;
temp = [NSString stringWithFormat:@"%c",[inputText characterAtIndex:i+j]];
checkString = [NSString stringWithFormat:@"%@%@",checkString,temp];
}
}
if ([checkString isEqualToString:@"www."] || [checkString isEqualToString:@"WWW."])
{
matched_FLAG = TRUE;
NSString *temp;
temp = [NSString stringWithFormat:@"%c",c];
startPos = [NSString stringWithFormat:@"%i",i];
returnURL = [NSString stringWithFormat:@"%@%@",returnURL,temp];
} else {
WWW_FLAG = FALSE;
}
}
else if (matched_FLAG) {
if (c != ' ' && c != '\n') {
NSString *temp;
temp = [NSString stringWithFormat:@"%c",c];
returnURL = [NSString stringWithFormat:@"%@%@",returnURL,temp];
if (i+1 == [inputText length]) {
matched_FLAG = FALSE;
WWW_FLAG = FALSE;
endPos = [NSString stringWithFormat:@"%i",i-1];
returnURL = [NSString stringWithFormat:@"%@ ,%@ ,%@",returnURL,startPos,endPos];
[returnUrlArray addObject:returnURL];
returnURL = @"";
}
} else if (c == ' ' || c== '\n') {
matched_FLAG = FALSE;
WWW_FLAG = FALSE;
endPos = [NSString stringWithFormat:@"%i",i-1];
returnURL = [NSString stringWithFormat:@"%@ ,%@ ,%@",returnURL,startPos,endPos];
[returnUrlArray addObject:returnURL];
returnURL = @"";
}
}
} // end WWW_FLAG
}// end for
return returnUrlArray;
}