S
S
Sergey Sulakov2015-10-08 13:58:24
PHP
Sergey Sulakov, 2015-10-08 13:58:24

How to replace a short tag with a file connection?

For example: in the text of an article there is such a short tag {module=name}
How to write a regular expression so that the 'name.php' file from the 'modules/' folder is included instead of the short tag?
If possible, the entire preg_replace function

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Khomenko, 2015-10-08
@web-verzus-team

$content = '... {module=module1} ... {module=header} ... {module=footer_box} ...';

$replaced = preg_replace_callback( '/{module=(.*)}/U', function ( $matches ) {
    $moduleName = $matches[ 1 ];

    // Делаем что нужно

    return '>>Заменили:' . $moduleName . '<<';
}, $content );

echo $replaced;

... >>Заменили:module1<< ... >>Заменили:header<< ... >>Заменили:footer_box<< ...

C
Cat Anton, 2015-10-08
@27cm

$text = preg_replace_callback ('/\{module\=(\w+)\}/i', function ($m) {
  return include "modules/$m[1].php"
}, $text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question