jk11uk Posted March 5, 2008 Share Posted March 5, 2008 hi, i want to extract strings from a page which are in the following format: <span class=a>STRING</span> so all different substrings from within the main string which start with <span class=a> and end with </span> would be captured. I came up with the following which doesn't seem to work at all preg_match_all('"^(<span class=a>)(</span>)$"', $mainstring, $match); //// print results to test echo $match[1]; any ideas? Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/ Share on other sites More sharing options...
fnairb Posted March 5, 2008 Share Posted March 5, 2008 Close.... preg_match_all('"<span class=a>(.+?)</span>"', $mainstring, $match); Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/#findComment-484035 Share on other sites More sharing options...
jk11uk Posted March 6, 2008 Author Share Posted March 6, 2008 thanks, however it doesn't seem to work properly. I did the following to print the results to test: print_r($match); and i get: Array ( [0] => Array ( ) [1] => Array ( ) ) an array with 2 arrays in? i'm after an array of strings. Any idea whats happening? Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/#findComment-484704 Share on other sites More sharing options...
jk11uk Posted March 6, 2008 Author Share Posted March 6, 2008 Any ideas? im really struggling(/stupid) ??? Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/#findComment-484751 Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 The array created by preb_match_all(), $match, in this case is a multidimensional array. preg_match_all() Documentation http://us2.php.net/manual/en/function.preg-match-all.php In this example... <?php $mainstring = "<span class=a>A</span><span class=a>B</span><span class=a>C</span>"; preg_match_all('"<span class=a>(.+?)</span>"', $mainstring, $match); print_r($match); ?> you would get output that looks like: Array ( [0] => Array ( [0] => <span class=a>A</span> [1] => <span class=a>B</span> [2] => <span class=a>C</span> ) [1] => Array ( [0] => A [1] => B [2] => C ) ) The array you are after is actually $matches[1]; To use those strings you could do something like: <?php echo "I found: ", implode(", ", $matches[1]), "\n"; ?> Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/#findComment-484865 Share on other sites More sharing options...
jk11uk Posted March 10, 2008 Author Share Posted March 10, 2008 awesome thanks, you're a legend Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/#findComment-488334 Share on other sites More sharing options...
jk11uk Posted March 10, 2008 Author Share Posted March 10, 2008 hmmmm. I tried your code and it doesn't seem to work. It doesn't work on it's own either ??? Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/#findComment-488457 Share on other sites More sharing options...
jk11uk Posted March 10, 2008 Author Share Posted March 10, 2008 sorry, my bad. Works beautifully. i was just being an idiot Link to comment https://forums.phpfreaks.com/topic/94526-regex-string-extract/#findComment-488461 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.