替换 str_replace 中的整个数字
Replace entire numbers in str_replace
我有一个包含数字的字符串,我需要对字符串中的每个数字进行颜色编码。
我的 preg_match_all 给了我一个包含正确数字 (5, 280, 51) 的数组。我需要将第一个数字涂成红色,第二个涂成蓝色,第三个涂成黑色。
目前,第三个数字 (51) 的着色不正确。 5 被着色为红色,然后 1 被忽略。我需要将整数 (51) 涂成黑色。
我相信我的代码匹配 5,然后匹配 280,然后匹配下一个 5,但没有考虑以下 1。我该如何解决这个问题?
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
preg_match_all('#\d+(?:\.\d{1,2})?#', $string, $matches);
for ($k = 0; $k < count($matches[0]); $k++)
{
$string = str_replace($matches[0][$k], '<strong><span style="color: '.$coloursArray[$k].';">' . $matches[0][$k] . '</span></strong>', $string);
}
输出:
dbase <strong><span style="color: red">5</span></strong>% used
- <strong><span style="color: blue">280</span></strong>G free
| audit <strong><span style="color: red">5</span></strong>1% used
最后一行应该是:
| audit <strong><span style="color: black">51</span></strong>% used
使用这个基于环视的正则表达式来避免匹配不需要的文本:
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
preg_match_all('#(?<!\d)\d+(?:\.\d{1,2})?(?!\d)#', $string, $matches);
for ($k = 0; $k < count($matches[0]); $k++) {
$string = preg_replace('/(?<!\d)' . $matches[0][$k] . '(?!\d)/',
'<strong><span style="color: '.$coloursArray[$k].';">' . $matches[0][$k] . '</span></strong>', $string);
}
echo $string;
输出:
base <strong><span style="color: red;">5</span></strong>% used - <strong><span style="color: blue;">280</span></strong>G free | audit <strong><span style="color: black;">51</span></strong>% used
第一次调用 str_replace
是替换字符串中的两个 5
。这意味着第三次调用什么都不做。
由于您的数组 $matches
包含您要替换的数字,您可以像这样使用 preg_replace
:
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
preg_match_all('#\d+(?:\.\d{1,2})?#', $string, $matches);
for ($k = 0; $k < count($matches[0]); $k++)
{
$string = preg_replace("/(\D){$matches[0][$k]}(\D)/", '<strong><span style="color: '.$coloursArray[$k].';">' . $matches[0][$k] . '</span></strong>', $string);
}
echo $string;
在这里,我使用 (\D)
捕获了每个数字前后的非数字字符,并使用 </code> 和 <code>
.[=22= 将它们添加到替换字符串中]
输出:
dbase <strong><span style="color: red;">5</span></strong>% used - <strong><span style="color: blue;">280</span></strong>G free | audit <strong><span style="color: black;">51</span></strong>% used
或者,您可以一次性完成所有操作,使用 preg_replace_callback
:
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
$c = 0;
$string = preg_replace_callback('#\d+(?:\.\d{1,2})?#', function($x) use (&$c, $coloursArray) {
return '<strong><span style="color: '.$coloursArray[$c++].';">' . $x . '</span></strong>';
}, $string);
每次匹配都传递给回调函数并执行替换,每次使用 $c++
到 select 来自数组的不同索引。
我有一个包含数字的字符串,我需要对字符串中的每个数字进行颜色编码。
我的 preg_match_all 给了我一个包含正确数字 (5, 280, 51) 的数组。我需要将第一个数字涂成红色,第二个涂成蓝色,第三个涂成黑色。
目前,第三个数字 (51) 的着色不正确。 5 被着色为红色,然后 1 被忽略。我需要将整数 (51) 涂成黑色。
我相信我的代码匹配 5,然后匹配 280,然后匹配下一个 5,但没有考虑以下 1。我该如何解决这个问题?
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
preg_match_all('#\d+(?:\.\d{1,2})?#', $string, $matches);
for ($k = 0; $k < count($matches[0]); $k++)
{
$string = str_replace($matches[0][$k], '<strong><span style="color: '.$coloursArray[$k].';">' . $matches[0][$k] . '</span></strong>', $string);
}
输出:
dbase <strong><span style="color: red">5</span></strong>% used
- <strong><span style="color: blue">280</span></strong>G free
| audit <strong><span style="color: red">5</span></strong>1% used
最后一行应该是:
| audit <strong><span style="color: black">51</span></strong>% used
使用这个基于环视的正则表达式来避免匹配不需要的文本:
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
preg_match_all('#(?<!\d)\d+(?:\.\d{1,2})?(?!\d)#', $string, $matches);
for ($k = 0; $k < count($matches[0]); $k++) {
$string = preg_replace('/(?<!\d)' . $matches[0][$k] . '(?!\d)/',
'<strong><span style="color: '.$coloursArray[$k].';">' . $matches[0][$k] . '</span></strong>', $string);
}
echo $string;
输出:
base <strong><span style="color: red;">5</span></strong>% used - <strong><span style="color: blue;">280</span></strong>G free | audit <strong><span style="color: black;">51</span></strong>% used
第一次调用 str_replace
是替换字符串中的两个 5
。这意味着第三次调用什么都不做。
由于您的数组 $matches
包含您要替换的数字,您可以像这样使用 preg_replace
:
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
preg_match_all('#\d+(?:\.\d{1,2})?#', $string, $matches);
for ($k = 0; $k < count($matches[0]); $k++)
{
$string = preg_replace("/(\D){$matches[0][$k]}(\D)/", '<strong><span style="color: '.$coloursArray[$k].';">' . $matches[0][$k] . '</span></strong>', $string);
}
echo $string;
在这里,我使用 (\D)
捕获了每个数字前后的非数字字符,并使用 </code> 和 <code>
.[=22= 将它们添加到替换字符串中]
输出:
dbase <strong><span style="color: red;">5</span></strong>% used - <strong><span style="color: blue;">280</span></strong>G free | audit <strong><span style="color: black;">51</span></strong>% used
或者,您可以一次性完成所有操作,使用 preg_replace_callback
:
$coloursArray = array('red', 'blue', 'black');
$string = "dbase 5% used - 280G free | audit 51% used";
$c = 0;
$string = preg_replace_callback('#\d+(?:\.\d{1,2})?#', function($x) use (&$c, $coloursArray) {
return '<strong><span style="color: '.$coloursArray[$c++].';">' . $x . '</span></strong>';
}, $string);
每次匹配都传递给回调函数并执行替换,每次使用 $c++
到 select 来自数组的不同索引。