codingmasterRS Posted March 7, 2011 Share Posted March 7, 2011 Hi guys I need to be able to extract (into an array) the content between two tags "<!--" and "-->" in a string ($string). I have $string = "<!--HELLO WORLD-->"; $tags = "/<!--(.*?)--!>/"; preg_match($tags, $string, $matches); return $matches[1]; but it does not seem to work, (no error code, nothing). Any help much appreciated, Link to comment https://forums.phpfreaks.com/topic/229837-get-content-between-tags/ Share on other sites More sharing options...
codingmasterRS Posted March 7, 2011 Author Share Posted March 7, 2011 And this one $pos = stripos($template, '<!--'); $str = substr($template, $pos); $str2 = substr($str, strlen('<!--')); $pos2 = stripos($str2, '-->'); $str3 = substr($str2, 0, $pos2); $unit = trim($str3); return $unit; Does not get an array of them, only gets the first one. Link to comment https://forums.phpfreaks.com/topic/229837-get-content-between-tags/#findComment-1183847 Share on other sites More sharing options...
codingmasterRS Posted March 7, 2011 Author Share Posted March 7, 2011 BUMP Link to comment https://forums.phpfreaks.com/topic/229837-get-content-between-tags/#findComment-1184021 Share on other sites More sharing options...
beegro Posted March 7, 2011 Share Posted March 7, 2011 To clarify: Are you wanting to get the contents, separated by a space, into an array from withing the "<!--" and "-->" strings? Link to comment https://forums.phpfreaks.com/topic/229837-get-content-between-tags/#findComment-1184024 Share on other sites More sharing options...
litebearer Posted March 7, 2011 Share Posted March 7, 2011 Perhaps... <?PHP $string = "<!--HELLO WORLD-->"; $tag1 = "<!--"; $tag2 = "-->"; $string = str_replace($tag1,"",$string); $string = str_replace($tag2,"",$string); $string_array = explode(" ", $string); print_r($string_array); ?> Link to comment https://forums.phpfreaks.com/topic/229837-get-content-between-tags/#findComment-1184039 Share on other sites More sharing options...
sasa Posted March 7, 2011 Share Posted March 7, 2011 you must escape 1st ! and remove 2nd one <?php $string = "<!--HELLO WORLD-->"; $tags = "/<\!--(.*?)-->/"; preg_match($tags, $string, $matches); print_r($matches[1]); ?> Link to comment https://forums.phpfreaks.com/topic/229837-get-content-between-tags/#findComment-1184203 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.