lfernando Posted December 17, 2010 Share Posted December 17, 2010 Hello, Does anyone know how to get a substrig by referencing other substrings around it? Ie, my string is the following: These are my favorite foods: <ul> <li id="1"> Pizza </li> <li id="2"> Burgers </li> <li id="three"> Hot Dogs </li> </ul> I need to get the ID of each list item, i know they will always follow "<li id=". I also need to get the text of each list item, i know they will always be before "</li>" Thanks! Link to comment https://forums.phpfreaks.com/topic/222012-get-substring-by-referencing-other-substrings-around-it/ Share on other sites More sharing options...
litebearer Posted December 17, 2010 Share Posted December 17, 2010 A very rough 'sledge-hammer' approach... <?PHP $string ='These are my favorite foods: <ul> <li id="1"> Pizza </li> <li id="2"> Burgers </li> <li id="three"> Hot Dogs </li> </ul>'; echo $string; $new_array = explode("<li id=", $string); $junk = array_shift($new_array); $string = implode("~",$new_array); $string = str_replace('"', "", $string); $string = str_replace('> ', "|", $string); $new_array = explode("~", $string); $i=0; $w=count($new_array); while($i<$w) { $t_array = explode("|",$new_array[$i]); echo "this is the id <B>" . $t_array[0] . "</b> and this is the accompanying text <i>" . $t_array[1] . "</i><br>"; $i ++; } ?> example output... http://nstoia.com/test/dog.php Link to comment https://forums.phpfreaks.com/topic/222012-get-substring-by-referencing-other-substrings-around-it/#findComment-1148809 Share on other sites More sharing options...
BlueSkyIS Posted December 17, 2010 Share Posted December 17, 2010 another option: <?php $string ='These are my favorite foods: <ul> <li id="1"> Pizza </li> <li id="2"> Burgers </li> <li id="three"> Hot Dogs </li> </ul>'; $pattern = "/<li id=\"(.*)\">(.*)<\/li>/"; $result = preg_match_all($pattern,$string,$matches); for ($i=0;$i<count($matches);$i++) { echo "id: {$matches[1][$i]} value: {$matches[2][$i]} <br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/222012-get-substring-by-referencing-other-substrings-around-it/#findComment-1148822 Share on other sites More sharing options...
desjardins2010 Posted December 17, 2010 Share Posted December 17, 2010 what does this line standfor? $pattern = "/<li id=\"(.*)\">(.*)<\/li>/"; thats giberish... Link to comment https://forums.phpfreaks.com/topic/222012-get-substring-by-referencing-other-substrings-around-it/#findComment-1148832 Share on other sites More sharing options...
BlueSkyIS Posted December 17, 2010 Share Posted December 17, 2010 yes, it's gibberish :-) um, it's a regular expression in case you are serious. Link to comment https://forums.phpfreaks.com/topic/222012-get-substring-by-referencing-other-substrings-around-it/#findComment-1148834 Share on other sites More sharing options...
BlueSkyIS Posted December 17, 2010 Share Posted December 17, 2010 in retrospect, i should have used single quotes on the outside of $pattern so to not have to escape double-quotes on the inside: $string ='These are my favorite foods: <ul> <li id="1"> Pizza </li> <li id="2"> Burgers </li> <li id="three"> Hot Dogs </li> </ul>'; $pattern = '/<li id="(.*)">(.*)<\/li>/'; $result = preg_match_all($pattern,$string,$matches); for ($i=0;$i<count($matches);$i++) { echo "id: {$matches[1][$i]} value: {$matches[2][$i]} <br />"; } Link to comment https://forums.phpfreaks.com/topic/222012-get-substring-by-referencing-other-substrings-around-it/#findComment-1148835 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.