PHP 查找并替换,将任何数字减半(例如 col-xs-24 到 col-xs-12)
PHP find and replace, halving any numbers (e.g. col-xs-24 to col-xs-12)
我们正在将 bootstrap 内容从 24 列迁移到 12 列。
所以我需要像这样转换一个字符串:
<div class="col-xs-24 col-md-12 col-lg-4">
至:
<div class="col-xs-12 col-md-6 col-lg-2">
我无法理解提取数字所需的 preg_replace,将其减半,然后将其放回原位。
感谢 u_mulder 建议 preg_replace_callback。
设法做到了……
$string = '<div class="col-xs-24 col-md-12 col-lg-4">';
$out = preg_replace_callback(
'/col\-\w{1,2}-(\d{1,2})/',
function ($matches) {
return str_replace($matches[1], ($matches[1] / 2), $matches[0]);
},
$string
);
我们正在将 bootstrap 内容从 24 列迁移到 12 列。
所以我需要像这样转换一个字符串:
<div class="col-xs-24 col-md-12 col-lg-4">
至:
<div class="col-xs-12 col-md-6 col-lg-2">
我无法理解提取数字所需的 preg_replace,将其减半,然后将其放回原位。
感谢 u_mulder 建议 preg_replace_callback。
设法做到了……
$string = '<div class="col-xs-24 col-md-12 col-lg-4">';
$out = preg_replace_callback(
'/col\-\w{1,2}-(\d{1,2})/',
function ($matches) {
return str_replace($matches[1], ($matches[1] / 2), $matches[0]);
},
$string
);