thehigherentity Posted May 27, 2006 Share Posted May 27, 2006 OK, im lost againI want to pill out a section of a string from between two different tagsI want to make a couple of functions. I need them to work by inputting the opening and closing tags eg [mytag] and [/mytag] with the strSo the first function would do something like Lets say “exreact_info” is the name of my function[code]$open_tag = “[mytag]”;$close_tag =”[/mytag]”;$str = “blah blah blah [mytag] info needed 1 [/mytag]blah blah blah[mytag]info needed 2[/mytag]blah blah blah[mytag]info needed 3[/mytag] blah blah blah”;$tagarray = exreact_info($str , $open_tag , $close_tag);[/code]I hope that this could then give me something like[code]$tagarray[0] // info needed 1$tagarray[1] // info needed 2$tagarray[2] // info needed 3[/code]Then i need a function to put the info back into one string again as it was before but with the edited information, something like the following[code]$str = “blah blah blah [mytag] info needed 1 * edited [/mytag]blah blah blah[mytag]info needed 2 * edited [/mytag]blah blah blah[mytag]info needed 3 * edited [/mytag] blah blah blah”;[/code]Im not asking someone to make these functions for me, I wont mind if someone does but I would like to know how to go about it.I first tried with explode and managed to get it kind of working but then found it was case sensitive and I need this to work with all variations eg [MyTaG] to work just like [mytag] etcI hope this all makes sense I have been up all night trying to get this code Finished and I’m just about asleep now Quote Link to comment https://forums.phpfreaks.com/topic/10557-extracting-information-from-a-string-and-replacing-it-again/ Share on other sites More sharing options...
eves Posted May 27, 2006 Share Posted May 27, 2006 use str_replace to deal with variations of [mytag]something like[code]$open_tag = "[mytag]";$close_tag ="[/mytag]";$str = "blah blah blah [mytag] info needed 1 [/mytag]blah blah blah[mytag]info needed 2[/mytag]blah blah blah[mytag]info needed 3[/mytag] blah blah blah";$var_open = array('[MyTag]','[myTag]','[MYTAG]');// include other variations$var_close = array('[/MyTag]','[/myTag]','[/MYTAG]');// include other variations$limit = count($variations);for($ctr=0;$ctr<$limit;$ctr++){ $str = str_replace($variations[$ctr],$open_tag,$str); $str = str_replace($variations[$ctr],$close_tag,$str);} [/code]then use explode to separate into chunks, retrieve the text in between the tagsedit them, then use the implode function to join them upsomething like:[code]$str_temp = explode('[mytag]',$str);$ret_val = "";$limit = count($str_temp);for($ctr=0;$ctr<$limit;$ctr++){ if (strpos($str_temp[$ctr],$close_tag)===false) { $ret_val .= $str_temp[$ctr]; } else { $tmp = explode($close_tag,$str_temp[$ctr]); $tmp[0] .= '(edited)'; // do your edit part here, $tmp[0] contains the text between the tags $ret_val .= $open_tag . implode($close_tag,$tmp); //join them up, append the open tag at the beginning } }[/code]hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/10557-extracting-information-from-a-string-and-replacing-it-again/#findComment-39394 Share on other sites More sharing options...
thehigherentity Posted May 27, 2006 Author Share Posted May 27, 2006 thanks for your quick reply on thisI never thought of thatI do have one question thoughIf i wanted to have many different tags and be able to extract them in this way is there any way I can go throught a string and lowercase anything between [ and ] ?this would then fix all my problems :) Quote Link to comment https://forums.phpfreaks.com/topic/10557-extracting-information-from-a-string-and-replacing-it-again/#findComment-39398 Share on other sites More sharing options...
thehigherentity Posted May 29, 2006 Author Share Posted May 29, 2006 I Thought I would let you all know how i fixed this problem, I made two functions one to extract the data.[code]function extract_from_tags($str, $open_tag, $close_tag){ $findopenings = explode($open_tag, $str); foreach($findopenings as $find_closings){ $taginfo = explode($close_tag, $find_closings); if (trim($taginfo[1]) == ""){ $after_tag[] = $taginfo[0]; $tag_info[] = " "; } else { $tag_info[] = $taginfo[0]; $after_tag[] = $taginfo[1]; } } $str = array($tag_info, $after_tag); return $str;}[/code]and one to place it back again[code]function replace_to_tags($tag_info, $outer_tag, $open_tag, $close_tag){ $str = ""; $info_count = count($tag_info); for ($i=0; $i<$info_count; $i++){ if ($i == 0){ if (trim($tag_info[$i]) == ""){ $tag_first = false; } else { $tag_first = true; } } if ($tag_first = true){ if (trim($tag_info[$i]) == ""){ $str .= $tag_info[$i].$outer_tag[$i]; } else { $str .= $open_tag.$tag_info[$i].$close_tag.$outer_tag[$i]; } } else { if (trim($tag_info[$i]) == ""){ $str .= $outer_tag[$i].$tag_info[$i]; } else { $str .= $outer_tag[$i].$open_tag.$tag_info[$i].$close_tag; } } } return $str;}[/code]I know its far from perfect code but it seems to work but here is how it works[code]$str = "[MYTAG]INFO NEEDED 1[/mytag]between 1 and 2[mytag]info from tag 2[/MYTAG]BETWEEN 2 AND £[mytag]info from tag 3[/mytag]from the end";$open_tag = "["; // opening tag$close_tag = "]"; // closing tag[/code]then call the function with [code]$str = extract_from_tags($str, $open_tag, $close_tag);[/code]this gives you a multidimensional arrayall the info between the tags would be on $str[0]and the rest would be on $str[1]so you could call it like $str[1][1] or $str[1][2] etcthis means I can edit them and then place them back into the array and run the following [code]$tag_info = $str[0];$outer_tag = $str[1];Str = replace_to_tags($tag_info, $outer_tag, $open_tag, $close_tag);[/code]To get my desired outputI hope this helps anyone else that has a similar problemIt seems to of solved all my problems so far making it easy to extract what ever I want and place it back were it was beforeCritics welcome, after all I’m only learning and as I said I know its far from perfect Quote Link to comment https://forums.phpfreaks.com/topic/10557-extracting-information-from-a-string-and-replacing-it-again/#findComment-39879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.