簡體   English   中英

為什么這部分 PHP 代碼不起作用?

[英]Why this portion of PHP code doesn't work?

我正在嘗試更改 substring 的顏色,如下所示:

$str = 'abcd<efgh>lmno';

preg_match_all('/<[\S]*?>/m', $str, $matches, PREG_PATTERN_ORDER);

$replacements = $needles = [];
foreach ($matches[0] as $match) {
    $needles[]      = $match;
    $replacements[] = '<span style="color:red;">' . $match . '</span>';
}

echo str_replace($needles, $replacements, $str);

我期待這個結果: abcd<span style="color:red;"><efgh></span>lmno但我得到: abcdlmno

嘗試這個:

$str = 'abcd<efgh>lmno';

preg_match_all('/<[\S]*?>/m', $str, $matches, PREG_PATTERN_ORDER);

foreach ($matches[0] as $match) {
    $str = str_replace($match, '<span style="color:red;">' . $match . '</span>', $str);
}

echo $str;
//abcd<span style="color:red;"><efgh></span>lmnoC

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM