[英]PHP preg_match and replace multiple instances between tags
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.所以在这个例子中,有 2 个产品,每个产品都有一个 Gallery 标签。 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
我希望能够找到并替换这些示例标签
[Gallery::product_id_01]
和[Gallery::product_id_02]
与实际图片库,这是通过名为ProdImgGallery()
的 php 函数完成的,它传递相同的 ID 示例: 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.标签将始终具有
[Gallery::*]
但由 * 指示的 :: 之后的文本将有所不同,需要从标签中捕获以供下一阶段使用。
Using preg_match_all("/\\[Gallery::([^\\]]*)\\]/", $blog_content, $product_id);
使用
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找到您期望的所有标签,但我需要用其唯一 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这是我所能得到的,但这只是根据
preg_match_all
找到的所有 id 获取每个画廊我不知道如何用相应的画廊替换每个画廊
//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!非常感谢!
You may try using preg_replace_callback
:您可以尝试使用
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.这里的想法是捕获
[Gallery::...]
每次出现,然后将捕获的组传递给回调函数。 The above script uses an inline anonymous function for the callback, which then returns the replacement you want, using the ProdImgGallery()
function.上面的脚本使用内联匿名函数进行回调,然后使用
ProdImgGallery()
函数返回您想要的替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.