0

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?

2
Contributors
3
Replies
19
Views
1 Year
Discussion Span
Last Post by cereal
0

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
        )

)
0

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);
0

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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.