Hello,

I am trying to create shortcodes in an easy way for my cms I am successfull to create and replace the shortcode with the content but what if there will be 2 shortcodes placed within the content I am confused to replace it if there are 2 shortcodes can anyone help me out with this please thank you for your help in advance

$content = "Hello world this is a lorem ipsum text [shortcode id='generate'] this is used for some [shortcode id='maximum'] dummy words";
 preg_match_all("/\[(.*?)\]/", $content, $matches);
 foreach($matches[1] as $match) {
    if(is_array($match)) {
        foreach($match as $value) {
            echo $value . "<br />";
            $id = explode('shortcode id=', $value);
        }
    } else {
        $match . "<br />";
        $id = explode('shortcode id=', $match);
        echo $id[1] . "<br />";
        $cleantext = str_replace("'", "", $id[1]);  
        $var = array($cleantext);
    }
 }
 echo str_replace("$cleantext", "Shortcode displayed", $content);

The problem is that I am really much confused about my code as well in this code it is only replacing it with the first one second one is not coming up as it is an array so can any one let me know how do I do this?

On line 3 you're looping index 1 of the $matches array:

foreach($matches[1] as $match)

So, due to the structure of this array, $match will be a string. Try this to get the values:

$values = array();

foreach($matches[1] as $match)
    $values[] = str_replace("'", "", explode('shortcode id=', $match)[1]);

But you can change the pattern and get those values through the regular expression:

preg_match_all("/\[shortcode id='(.*?)'\]/", $content, $matches);

The content of $matches should look like:

Array
(
    [0] => Array
        (
            [0] => [shortcode id='generate']
            [1] => [shortcode id='maximum']
        )

    [1] => Array
        (
            [0] => generate
            [1] => maximum
        )

)

Thank you it worked for me but I updated code code before when i was echoing the value it was showing me llike thi [shortcode id='Shortcode displayed']

so I changed the code and here is my updated one please let me know if this is correct as the output is coming okay

$content = "Hello world this is a lorem ipsum text [shortcode id='generate'] this is used for some [shortcode id='maximum'] dummy words";
 preg_match_all("/\[(.*?)\]/", $content, $matches);

 $values = array();

 foreach($matches[1] as $match) {
    if(is_array($match)) {
        foreach($match as $value) {
            echo $value . "<br />";
            $id = explode('shortcode id=', $value);
        }
    } else {
        echo $values[] = str_replace($match, "[$match]", $match);
        echo "<br />";
    }
 }
 echo str_replace($values, "Shortcode displayed", $content);

As I see it, this is replacing only the last shortcode. If the goal is to replace all the [shortcode ...] blocks with Shortcode displayed, then preg_replace() should work fine:

$matches = preg_replace("/\[(.*?)\]/", "Shortcode displayed", $content);
echo $matches;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.