preg_match_all result = "Array"

Hi all,

Got a quick question for you this time.

From the below preg_match_all() will always return a value of “Array” regardless what I try. To put this into plain code terms, $content will always = “Array”.

I have tested whether it’s getting the right input and it is. There are definetly [test123] and [/test123] custom tags in the php file it’s reading into $buffer which later gets passed into preg_match_all() as $current_line. It should spit out whatever it between these tags.

The bug has got to be in the preg_match_all() right? Thanks in advance.

Here’s the code:

function get_all_content_between($string, $start, $end)
{
preg_match_all( ‘/$start(.*)$end/s’, $string, $match );
return $match[1];
}

$rss_source_file = fopen(“$rss_from_file”, “r”) or die(“can’t open file [SOURCE]”);
$rss_write_file = fopen(“$rss_to_file”, “a”) or die(“can’t open file [DESTINATION]”);

while (!feof ($rss_source_file))
{
	$buffer = fgets($rss_source_file);
	$lines[] = $buffer;
} 
$array_count = count($lines);


$matchtag_start = "[test123]";
$matchtag_end = "[/test123]";
for ($i = 0; $i < $array_count; $i++) 
{	
	$current_line = $lines[$i];
	$content =  get_all_content_between($current_line, '\\(\\$matchtag_start\\(', "\\)\\$matchtag_end\\)"); 
	
	print $content;
					
		fwrite($rss_write_file,  $content ."\\r\

");

}

fclose($rss_source_file);
fclose($$rss_write_file);

preg_match_all returns an array of arrays.

Use var_dump to see what’s going on.

That’s because it returns an array. If you echo an array it will simply output “Array”.

Try using var_dump to see what is output, then model your code around that.

Edit:

Beaten to it! Good call.

Thanks guys.

Now it’s printing “array(0) { }” which would suggest it’s not even finding the [123test] whatever goes here [/123test] in $current_line

I was thinking this was because [123test] and [/123test] might be on a separate line but I’m reading the file in as one line anyway. The input is good as if I just print the input as I read it then it will simply render the source page inside the current php file (where the script is run).

It’s got to be something with /$start(.*)$end/s in preg_match_all() as I can’t picture it any other way.

…or it could be the way I’ve passed ‘\(\$matchtag_start\(’, “\)\$matchtag_end\)” in get_all_content_between().

You need to make sure that the regex string you pass to preg_match_all is both valid, and what you want. There’s lots of special characters in regular expressions. [ and ] are special. They define a character class. You can’t just allow put anything you want into a regex and expect it to work.

You could make use of preg_quote() to ensure the start and end strings are interpreted literally.

You could also just not use regular expressions. You can easily do the same with a combination of strpos(), strlen(), and substr()

Thanks. So far I’ve taken your first comment onboard and tried the below, sadly this may have been a further bug however the main bug is still in there.

$content = get_all_content_between($current_line, ‘\(\preg_quote($matchtag_start)\(’, “\)\preg_quote($matchtag_end)\)”);

I’m aware about doing it via the strxxx functions but also have come to learn that they’re a lot slower than preg_match_all() which is why I’m trying to stick to it if possible.

I’ve done a search for examples online that also use [whatever here ] custom tags with preg_match_all() and if there is a difference between how I’m passing the strings and how they are then it’s very minute else I like to believe my eyes would spot it by now.