mkosmosports Posted May 20, 2007 Share Posted May 20, 2007 Hey, Ive got a file which contains around 150 lines of the following html: <option value="56">Team Canada</option> What I need is to transform each of these lines to be: <div class="teamlist"><a href="team.php?team=56" target="_blank">Team Canada</a></div> Im thinking the easiest way to accomplish this is to get "Team Canada" and "56" into an array, where "56" would be the associative key to "Team Canada". Any ideas how I could accomplish this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/52230-file-string-manipulation/ Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 <?php function textbetweenarray($s1,$s2,$s){ $myarray=array(); $s1=strtolower($s1); $s2=strtolower($s2); $L1=strlen($s1); $L2=strlen($s2); $scheck=strtolower($s); do{ $pos1 = strpos($scheck,$s1); if($pos1!==false){ $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false){ $myarray[]=substr($s,$pos1+$L1,$pos2); $s=substr($s,$pos1+$L1+$pos2+$L2); $scheck=strtolower($s); } } } while (($pos1!==false)and($pos2!==false)); return $myarray; } $source = file_get_contents("html.html"); $options = textbetweenarray("<option ", "/option>", $source); foreach($options as $option) { $id = textbetweenarray("value=\"", "\"", $option); $id = $id[0]; $teamname = textbetweenarray(">", "<", $option); $teamname = $teamname[0]; echo "<div class=\"teamlist\"><a href=\"team.php?team=$id\" target=\"blank\">$teamname</a></div>\n"; } ?> Not tested, should work though Quote Link to comment https://forums.phpfreaks.com/topic/52230-file-string-manipulation/#findComment-257658 Share on other sites More sharing options...
mkosmosports Posted May 20, 2007 Author Share Posted May 20, 2007 Beautiful! This works great chigley. Now I just gotta break it down and understand it better.... I appreciate your help and time! Quote Link to comment https://forums.phpfreaks.com/topic/52230-file-string-manipulation/#findComment-257676 Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 You're welcome I'm sure there's an easier way using regex, but I find it hard to understand so I just stick to that function. Good luck with your coding Quote Link to comment https://forums.phpfreaks.com/topic/52230-file-string-manipulation/#findComment-257678 Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 try <?php $data = 'blare <option value="56">Team Canada</option> ghfdsgfh gfj dsfj blare <option value="52">Team USD</option> ghfdsgfh gfj dsfj blare <option value="55">Team England</option> ghfdsgfh gfj dsfj blare <option value="56">Team france</option> ghfdsgfh gfj dsfj '; echo preg_replace('%<option value="(\d+)">(.*)</option>%i', '<div class="teamlist"><a href="team.php?team=$1" target="_blank">$2</a></div>', $data ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52230-file-string-manipulation/#findComment-257692 Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 How did I know someone would come and completely blow my version out the window.. Quote Link to comment https://forums.phpfreaks.com/topic/52230-file-string-manipulation/#findComment-257696 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.