1

Sorry if the title is a little confusing its hard to explain so will try my best :)

Ok I have a blog and in the back end I want to be able to add multiple galleries for each product in a post

Example blog:

Some Product Title 1
blah blah blah blah
[Gallery::product_id_01]

Some Product Title 2
blah blah blah blah
[Gallery::product_id_02]

So in this example, there are 2 products each with a Gallery tag. I want to be able to find and replace these example tags [Gallery::product_id_01] and [Gallery::product_id_02] with the actual image gallery, which is done via a php function called ProdImgGallery() this passes the same ID example: product_id_01

The tags will always have [Gallery::*] but the text after the :: indicated by * will be different and needs to be captured from the tag for the next stage.

Using preg_match_all("/\[Gallery::([^\]]*)\]/", $blog_content, $product_id); finds all the tags as you would expect but I then need to replace the tags with the correct gallery indicated by its unique ID

This is as far as I can get but this just fetches each gallery based on all the ids found by preg_match_all I cant figure out how to replace each one with the corresponding gallery

//Find all Matches and put in array    
preg_match_all("/\[Gallery::([^\]]*)\]/", $blog_content, $product_id);

//Count all matches
$all_matches = count($product_id[1]);

//Loop through them
for($x=0;$x<$all_matches;$x++)
{
echo $product_id[1][$x]."<br>";
$prod_img_gallery = ProdImgGallery($product_id[1][$x]);
}
$blog_content = preg_replace("/\[Gallery::([^\]]*)\]/",$prod_img_gallery,$blog_content);

Hope this makes some kind of sense, I have trouble explaining things so please forgive me :) I did try searching too but could not find an answer that matched my exact problem

Many thanks!

4
  • is this PHP? If so, it will be helpful if you use the tag PHP Additional question: Why can't you use "[Gallery::product_id_01]" as your regular expression? e.g. for(...) { $target_id=sprintf("/[Gallery::product_id_%02d]/", id); } Commented Jan 2, 2020 at 9:00
  • @Popeye hi sorry im a noob here and didnt realise - my bad - I cant use "[Gallery::product_id_01]" as this will be dynamic Commented Jan 2, 2020 at 9:02
  • i am sorry that i hit save button earlier than I finished my --answer-- question. I do understand the point of being random. Can you give me a real example of your random line? Commented Jan 2, 2020 at 9:09
  • 1
    @Popeye I have said in the post its will be like '[Gallery::product_id_02]' but the id after '::' will be dynamic so for example in the back end when writting the blog I click a button and it will add '[Gallery::] ' I just then enter the product id for example '[Gallery::product_id_02] ' I would be adding say about 10 products on a blog post so there will be a set of 10 of these codes all with unique ids - hope that makes sense Commented Jan 2, 2020 at 9:17

1 Answer 1

1

You may try using preg_replace_callback:

$input = "Some Product Title 1
blah blah blah blah
[Gallery::product_id_01]

Some Product Title 2
blah blah blah blah
[Gallery::product_id_02]";

$out = preg_replace_callback(
    "/\[Gallery::(.*?)\]/", function($m) {
                                $val = "[Gallery::" . ProdImgGallery($m[1]) . "]";
                                return $val;
                            },
    $input);
echo $out;

The idea here is to capture every occurrence of [Gallery::...], and than pass the captured group into a callback function. The above script uses an inline anonymous function for the callback, which then returns the replacement you want, using the ProdImgGallery() function.

Sign up to request clarification or add additional context in comments.

1 Comment

many thanks after a little play with this its works!!! - I have to edit $val = "[Gallery::" . ProdImgGallery($m[1]) . "]"; to $val = ProdImgGallery($m[1]) ; to remove the [Gallery part as it was appearing as text. I will do a full test to make sure - Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.