我正在尝试更改由* *字符标识的字符串($ nota)中单词的字体颜色。单词存储在XML文件中。如下所示:
<vocabulario id="01">
<nota>I got *something* for you</nota>
</vocabulario>
<vocabulario id="02">
<nota>This *gift* is for you</nota>
</vocabulario>
<vocabulario id="01">
<nota>I got *something* for you</nota>
</vocabulario>
<vocabulario id="03">
<nota>Nice *ball*</nota>
</vocabulario>
上面这些字符串中的东西,礼物和球这些词在两个*
之间,并且应该以其余词组的不同颜色显示。同时,应该擦除/替换*
字符以使其不显示在页面上(输出)。
预期结果:“东西”,“礼物”,“球”应在页面(输出)上显示为红色。存储在* *字符中的每个新条目均应以红色显示。
这是我的代码,但是没有按我期望的那样工作:
<?php
$new_text = preg_replace('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', '<span style="color:red;">$1</span>', $nota);
?>
使用
$path_to_file = 'path/to/the/file';
$file_contents = file_get_contents($path_to_file);
$file_contents = preg_replace("/\*([^*]+)\*/", '<span style="background:#FFFF00;">$1</span>', $file_contents);
file_put_contents($path_to_file,$file_contents);
我做到了!我找到了解决方案。签出: