调用函数而不是在 preg_replace_callback 中创建函数
Call a function not create one in preg_replace_callback
我正在尝试将我的旧 preg_replace
转换为 preg_replace_callback
,但非常困难。想知道是否有人可以提供帮助。
我的旧工作:
$pattern = "/\[product\](.+?)\[\/product\]/e";
$text = "Hejsan hoppsan [product]2022|lots of text textext[/product] och sen så händer det lite blandat och så har vi [product]11245| med en jävla massa text[/product]";
echo preg_replace($pattern, "productBox('')", $text);
只要在 |
之后和 [/product]
.
之前没有新行,这工作得很好
[product]2022| like
this
dont
work[/product]
但除此之外代码工作正常。
函数:
function productBox($text) {
global $wpdb;
$test = explode("|",$text);
$loopProductID = $test[0];
$desc = $test[1];
//sql's to get information about the products
$productHTML = '<div class="flow-item">
<div class="flow-product-image"><a href="'.$permalink.'"><img src="'.$thumburl.'" alt="'.$title.'" /></a></div>
<div class="flow-product-info">
<h3><a href="'.$permalink.'">'.$title.'</a></h3>
<div style="padding: 5px 0px 10px;">'.$desc.'</div>
<div class="flow-product-facts">Art.nr: '.$artnr.' - Pris: '.$prisinklmoms.' kr</div>
</div>
<div class="clear"></div>
</div>';
return $productHTML;
}
我还尝试将整个函数放在 preg_replace_callback
中,像这样:
$test = preg_replace_callback($pattern,
function productBox($matches) {
global $wpdb;
$test = explode("|",$matches);
$loopProductID = $test[0];
$desc = $test[1];
//sql's to get information about the products
$productHTML = '<div class="flow-item">
<div class="flow-product-image"><a href="'.$permalink.'"><img src="'.$thumburl.'" alt="'.$title.'" /></a></div>
<div class="flow-product-info">
<h3><a href="'.$permalink.'">'.$title.'</a></h3>
<div style="padding: 5px 0px 10px;">'.$desc.'</div>
<div class="flow-product-facts">Art.nr: '.$artnr.' - Pris: '.$prisinklmoms.' kr</div>
</div>
<div class="clear"></div>
</div>';
return $productHTML;
}
, $text);
Returns 一个白页。
//sql's to get information about the products
包含相当多的 sql,所以我把它拿走了,因为它会很长 post 而 sql 工作正常。
有人可以帮我吗?
最好的解决方案是如果我可以在不在 preg_replace_callback
中创建函数的情况下使用它,因为代码将在更多地方使用并且一遍又一遍地创建函数似乎很糟糕,以防万一必须改变其中的一些东西。所以我宁愿只调用函数。
或者有什么办法可以同时做吗?把整个东西放在一个函数中,然后像 myGoodFunction($text_I_want_fixed);
?
这样调用它
[product]productid|Text about the product to show[/product]
这就是它现在的制作方式,我一直在考虑尝试更改正则表达式以使其像 [product id="productid"]Text[/product]
但那是以后的修复。只是想弄清楚如何修复 preg_replace_callback
.
在此先感谢您的帮助!
您需要传递一个匿名函数,而不是命名函数:
preg_replace_callback($pattern, function ($matches) { ... }, $text);
// look ma, no name! ^
否则就是语法错误
另一种方法是定义一个命名函数并将其作为回调传递:
function productBox($matches) { ... }
preg_replace_callback($pattern, 'productBox', $text);
// pass a callback by name ^
我正在尝试将我的旧 preg_replace
转换为 preg_replace_callback
,但非常困难。想知道是否有人可以提供帮助。
我的旧工作:
$pattern = "/\[product\](.+?)\[\/product\]/e";
$text = "Hejsan hoppsan [product]2022|lots of text textext[/product] och sen så händer det lite blandat och så har vi [product]11245| med en jävla massa text[/product]";
echo preg_replace($pattern, "productBox('')", $text);
只要在 |
之后和 [/product]
.
[product]2022| like
this
dont
work[/product]
但除此之外代码工作正常。
函数:
function productBox($text) {
global $wpdb;
$test = explode("|",$text);
$loopProductID = $test[0];
$desc = $test[1];
//sql's to get information about the products
$productHTML = '<div class="flow-item">
<div class="flow-product-image"><a href="'.$permalink.'"><img src="'.$thumburl.'" alt="'.$title.'" /></a></div>
<div class="flow-product-info">
<h3><a href="'.$permalink.'">'.$title.'</a></h3>
<div style="padding: 5px 0px 10px;">'.$desc.'</div>
<div class="flow-product-facts">Art.nr: '.$artnr.' - Pris: '.$prisinklmoms.' kr</div>
</div>
<div class="clear"></div>
</div>';
return $productHTML;
}
我还尝试将整个函数放在 preg_replace_callback
中,像这样:
$test = preg_replace_callback($pattern,
function productBox($matches) {
global $wpdb;
$test = explode("|",$matches);
$loopProductID = $test[0];
$desc = $test[1];
//sql's to get information about the products
$productHTML = '<div class="flow-item">
<div class="flow-product-image"><a href="'.$permalink.'"><img src="'.$thumburl.'" alt="'.$title.'" /></a></div>
<div class="flow-product-info">
<h3><a href="'.$permalink.'">'.$title.'</a></h3>
<div style="padding: 5px 0px 10px;">'.$desc.'</div>
<div class="flow-product-facts">Art.nr: '.$artnr.' - Pris: '.$prisinklmoms.' kr</div>
</div>
<div class="clear"></div>
</div>';
return $productHTML;
}
, $text);
Returns 一个白页。
//sql's to get information about the products
包含相当多的 sql,所以我把它拿走了,因为它会很长 post 而 sql 工作正常。
有人可以帮我吗?
最好的解决方案是如果我可以在不在 preg_replace_callback
中创建函数的情况下使用它,因为代码将在更多地方使用并且一遍又一遍地创建函数似乎很糟糕,以防万一必须改变其中的一些东西。所以我宁愿只调用函数。
或者有什么办法可以同时做吗?把整个东西放在一个函数中,然后像 myGoodFunction($text_I_want_fixed);
?
[product]productid|Text about the product to show[/product]
这就是它现在的制作方式,我一直在考虑尝试更改正则表达式以使其像 [product id="productid"]Text[/product]
但那是以后的修复。只是想弄清楚如何修复 preg_replace_callback
.
在此先感谢您的帮助!
您需要传递一个匿名函数,而不是命名函数:
preg_replace_callback($pattern, function ($matches) { ... }, $text);
// look ma, no name! ^
否则就是语法错误
另一种方法是定义一个命名函数并将其作为回调传递:
function productBox($matches) { ... }
preg_replace_callback($pattern, 'productBox', $text);
// pass a callback by name ^