Saypontigohe Saypontigohe - 23 days ago 9
PHP Question

Store variable from preg_split, and use it it comination with the word

I am still facing a major problem, which in a previous question was unfortunally not answered. I want to do the following:

I have a custom made cms, where i want to use shortcodes to use plugins. It is working like this:

this is a text
[plugin-blog]
this is more text
and some more
[plugin-contact]


What it outputs is this:

this is a text
include_once('plugins/blog/blog.php'); <- loads specific file for plugin
this is more text
and some more
include_once('plugins/contact/contact.php'); <- loads specific file for plugin


This is working fine, but what i want is to have a specific id from the plugin, which i want to call with
[plugin-blog 2]
. I need to store the number of the plugin and use it later inside the php file of the plugin like this
SELECT * FROM 'plugin_db' WHERE 'id' = $value_from_shortcode


How can I do this? I am totally blank on how to approach this problem. I tried literally everything, but either the loop is not working anymore, of the number is not saved with the comination of the plugin name. I have the following code:

$regex = '~\[plugin-([^]]+)]~';
$content_one = htmlspecialchars_decode($page['content_one']);
$parts = preg_split($regex, $content_one, -1, PREG_SPLIT_DELIM_CAPTURE);

foreach($parts as $k => $v){
if($k & 1)
include_once('plugins/'.$v.'/'.$v.'.php');
else
echo htmlspecialchars_decode($v);
}

Answer

You can retrieve the id and the name with:

    foreach($parts as $k => $v){
        if($k & 1) {
            $id = preg_replace('/\D+/', '', $v);
            $v = preg_replace('/\d+/', '', $v);
            echo "id = $id\n";
            include ('plugins/'.$v.'/'.$v.'.php');
        } else {
            echo htmlspecialchars_decode($v);
        }
    }

Note: keep the test $k & 1 to deal with all plugins.