Jump to content

preg_match


doddsey_65

Recommended Posts

If you want to run PHP code on the replacement you'll need to use the e pattern modifier.

$match = preg_replace('|test-([0-9])|e', "$array[\\1]", $content);

 

EDIT Just been testing my suggestion and I think you may need to use preg_replace_callback instead.

Link to comment
https://forums.phpfreaks.com/topic/235857-preg_match/#findComment-1212394
Share on other sites

thanks for the reply. this is what im using but i get an error:

 

$content = preg_replace('|\<asf: var\[(.*?)\]\>|e', "{$array['\\1']}", $content);

 

the error is undefined index \1

 

so its not working as i thought.

 

I used your code:

 

$content = preg_replace('|\<asf: var\[(.*?)\]\>|e', "$array[\\1]", $content);

 

and got a parse error. where am i going wrong?

 

 

Link to comment
https://forums.phpfreaks.com/topic/235857-preg_match/#findComment-1212397
Share on other sites

I don't thing you can use the matches returned from regex to call a variable/array item. Instead you'll need to use preg_replace_callback. Example

$array[1] = 'test';
$array[23] = 'foobar';

$content = 'test-23<br /><br />test-1';

$match = preg_replace_callback('|test-([0-9]+)|',
                                create_function('$matches', 'global $array; return $array[$matches[1]];'),
                                $content);

Link to comment
https://forums.phpfreaks.com/topic/235857-preg_match/#findComment-1212404
Share on other sites

thanks for the help. Im using this to start my own MVC but there is one thing left. What im doing is taking a file which has content like:

 

$title = 'This is a test';

$array = array('title' => $title);

$template->render('./test_template_tpl.php', $array);

 

the render function then looks for content like:

 

<asf: var[title]>

 

in the tpl file and replaces it with the array item that equals var[title].

 

It then echos the array item, which works fine. But it is then supposed to write the new content to a cache file. I have another example which does that:

 

public function render($path, $array)
    {
        $file = fopen($path, 'r');
        
        $content = fread($file, filesize($path));

        $content = preg_replace('|\<asf: header\[(.*?)\]\>|', '<div class="header">\\1</div>',$content);
        
        $cache_file = './cache/cache_test.php';
        $file = fopen($cache_file, 'w');
        
        fwrite($file, $content);
        return include($cache_file);
        fclose($file);

 

that turns:

 

<asf: header[testing]>

 

into this and writes it to the new cache file:

 

<div class="header">Testing</div>

 

but the <asf: var[title]> tag still appears in the cache file when i need it to be overwritten with the array item.

 

Anyone follow?

Link to comment
https://forums.phpfreaks.com/topic/235857-preg_match/#findComment-1212410
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.