PHP preg_match_all runs php function for matched elements

I am not sure of the terminology, so I apologize in advance.

I am trying to create a PHP template engine that will query the string for <ZONE header>and </ZONE header>, and it will pull everything in between and then run the php function to see if the header exists. If the title exists, it will display what was between them, and if the title does not exist, it will delete what was between them.

Here is an example:

$string = "
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<ZONE header><img src="images/header.jpg" /></ZONE header>
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>
";

The function will delete perfectly <ZONE header><img src="images/header.jpg" /></ZONE header>, then it will launch the php function header()that I created , which checks if the header exists in the database, and if so, it displays everything inside <ZONE header></ZONE header>, and if it does not remove it from the line.

If a header exists:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<img src="images/header.jpg" />
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>

"header" :

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>

, :

        preg_match_all("|\<ZONE_header>(.*)\<\/ZONE_header>|isU", $string, $zone, PREG_SET_ORDER);

        if (isset($zone) && is_array($zone)) {
            foreach ($zone as $key => $zoneArray) {
                if ($key == 0) { 
                    $html = $zoneArray[1];
                    if ($html != "") {
                        if (header() != "") {
                            $html = str_replace($zoneArray[0], NULL, $html);
                        }
                    }                       
                }
            }
        }

        echo $html;

, , ? !

+3
2

, header() get_header().

$string = preg_replace_callback('/<ZONE header>(.+)<\/ZONE header>/', 'replace_header', $string);

function replace_header($matches) {
  return get_header() ? $matches[1] : '';
}

. preg_replace_callback.

0

?

$string = '
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<ZONE header><img src="images/header.jpg" /></ZONE header>
<p>Nam sollicitudin mattis nisi, eu convallis mi tincidunt vitae.</p>
';
$pattern="#<ZONE header[^>]*>(.+)</ZONE header>#iU"; 

preg_match_all($pattern, $string, $matches);
if (strlen($matches[0][0])==0){
    $string=strip_tags($string,"<p>");
}
else{
    $string=strip_tags($string,"<p><img>");

}
echo $string;
0

All Articles