troubleshooting Question

Regex confusion

Avatar of Derokorian
DerokorianFlag for United States of America asked on
Web DevelopmentPHPRegular Expressions
10 Comments3 Solutions514 ViewsLast Modified:
So I have the following regex pattern

$pattern = '#\[PHP](.*)\[\/PHP\]#i';

Which seems to work as long as there are no line breaks in between the php tags and I'm super confused why a line break would... break it. For example this works:

<?php

$str = <<<'STR'
I wrote this function to check if a table exists in the database or not.

[php]if( !is_object($db) || get_class($db) != 'mysqli' ) { // check if $db is a valid MySQLi resource[/php]

Thanks so much for looking at my code!
STR;

$pattern = '#\[PHP](.*)\[\/PHP\]#i';

if( preg_match_all($pattern,$str,$matches) ) {
	var_dump($matches);
	$output = 'SUCCESS';
} else {
	$output = 'FAILED';
}
echo $output;

But this does not. Notice the only difference is the line break (I've also tried two letters and two letters with a LB between them and got the same problem)

<?php

$str = <<<'STR'
I wrote this function to check if a table exists in the database or not.

[php]// check if $db is a valid MySQLi resource
if( !is_object($db) || get_class($db) != 'mysqli' ) { [/php]

Thanks so much for looking at my code!
STR;

$pattern = '#\[PHP](.*)\[\/PHP\]#i';

if( preg_match_all($pattern,$str,$matches) ) {
	var_dump($matches);
	$output = 'SUCCESS';
} else {
	$output = 'FAILED';
}
echo $output;
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 3 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros