开发者

PHP: Trouble with preg_match_all() return

开发者 https://www.devze.com 2023-01-05 10:16 出处:网络
I\'m writing this quick script to extract chatroom names from the source of a webpage. I\'m grabbing the data with fopen() and fgets() and that all returns fine.

I'm writing this quick script to extract chatroom names from the source of a webpage. I'm grabbing the data with fopen() and fgets() and that all returns fine.

My regex is /#[a-zA-z]+/, which does seem to wor开发者_运维知识库k. However I can not get preg_match_all() to return a concise list of data.

preg_match_all("/#[a-zA-z]+/", $contents, $foo, PREG_SET_ORDER);
foreach($foo as $item) print $item;

Returns "ArrayArrayArrayArrayArrayArrayArrayArray...".

print_r ($item);

Returns something along ( [0] => #channel1 ) Array ( [0] => #channe2 ) Array ( [0] => #channel3 )...

I'm unsure how to get this formated properly, any quick help?


$contents = '#channel1 #channel2 #channel3';

preg_match_all("/(#[a-zA-z0-9]+)/", $contents, $foo, PREG_SET_ORDER);
var_dump($foo);
echo '<hr />';

foreach($foo as $item)
   print $item[1].'<br />';


Walked away and came back and it was pretty clear what was up. The desired values in the $foo array were wrapped in another array. I simply wrapped my foreach() statement with another foreach() statement thusly:

foreach($foo as $item) {
   foreach($item as $bar) {
     print $bar . "<br />";
   }
}

Worked without a hitch.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号