desithugg Posted May 13, 2007 Share Posted May 13, 2007 Umm, I was wondering if I could use php and get certain parts of a string like $string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; Would it be possiable to use something like preg match to get only that part Desithugg into another string like $name. So I want to get the part between <td>Name</td><td> and </td></tr>. If it's possiable can anyone give me an example. I tried looking at the manual but didn't quite get it. Link to comment https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/ Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 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; } $string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; $tds = textbetweenarray("<td>", "</td>", $string); echo $tds[1]; ?> $tds -> array with each occurence of text between each <td> Link to comment https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/#findComment-252085 Share on other sites More sharing options...
per1os Posted May 13, 2007 Share Posted May 13, 2007 Nice little function Chig. To end the question on how to assign $name that value try this: <?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; } $string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; $tds = textbetweenarray("<td>", "</td>", $string); $$tds[0] = $tds[1]; // assigns the value of $tds[0] (name the value of $tds[1]) echo $Name; ?> =) Link to comment https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/#findComment-252087 Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 2007 To add: the best thing to do when parsing table rows is this: <?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; } $string = "<table><tr><td>Name</td><td>Desithugg</td></tr></table>"; $trs = textbetweenarray("<tr>", "</tr>", $string); foreach($trs as $tr) { $tds = textbetweenarray("<td>", "</td>", $tr); foreach($tds as $td) { // Do something } } ?> Link to comment https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/#findComment-252089 Share on other sites More sharing options...
desithugg Posted May 13, 2007 Author Share Posted May 13, 2007 thnx, exactly what I needed Link to comment https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/#findComment-252090 Share on other sites More sharing options...
desithugg Posted May 13, 2007 Author Share Posted May 13, 2007 Umm a lil problem I only gave, the function works fine but the thing is I don't want stuff between evry <td> and </td> Maybe I didn't give a good example, I want to extract a number of strings from the big string, but i only put one in the example. <? $string = "<table><tr><td>Name</td><td>Desithugg</td></tr> <tr><td>Age</td><td>14</td></tr> <tr><td>Gender</td><td>Male</td></tr></table>"; ?> So lets say I want to extract only the answers. so I want what's between <tr><td>Name</td><td> and </td></tr><tr><td>Age</td> in the string $answers['0']; than i want what's between <tr><td>Age</td><td> and </td></tr><tr><td>Gender</td> in the string $answers['1']; and what's between <tr><td>Gender</td><td> and </td></tr></table> in the string $answers['2']; So basically I want to run the function number of times and each time looking for something different. I tried to run the function you gave me twice but didn't work, I think it just looks for stuff between every <td> and </td> But I don't want to get stuff between every one of them, Because there will be some info that I don't need. Link to comment https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/#findComment-252106 Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 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; } $string = "<table><tr><td>Name</td><td>Desithugg</td></tr> <tr><td>Age</td><td>14</td></tr> <tr><td>Gender</td><td>Male</td></tr></table>"; $output = array(); $trs = textbetweenarray("<tr>", "</tr>", $string); foreach($trs as $tr) { echo $tr; $tds = textbetweenarray("<td>", "</td>", $tr); $first = $tds[0]; $second = $tds[1]; $output[$first] = $second; } echo "<pre>"; foreach($output as $k => $v) { echo "$k = $v\n"; } echo "/<pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/51195-php-get-part-of-a-string/#findComment-252113 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.