Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php preg_replace()在**字符之间更改单词的文本颜色_Php_Html_Preg Replace - Fatal编程技术网

php preg_replace()在**字符之间更改单词的文本颜色

php preg_replace()在**字符之间更改单词的文本颜色,php,html,preg-replace,Php,Html,Preg Replace,我正在尝试更改由**字符标识的字符串($nota)中单词的字体颜色。这些单词存储在一个XML文件中。如下所示: <vocabulario id="01"> <nota>I got *something* for you</nota> </vocabulario> <vocabulario id="02"> <nota>This *gift* is for you</nota> </vocab

我正在尝试更改由**字符标识的字符串($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>
使用

$path_to_file='path/to/the/file';
$file\u contents=file\u get\u contents($path\u to\u file);
$file\u contents=preg\u replace(“/\*([^*]+)\*/”、“$1”、$file\u contents);
文件内容($path\u to\u file,$file\u contents);
请参阅和


\*
匹配
*
字符,
([^*]+)
匹配并捕获组1中除星号以外的一个或多个字符,然后匹配
*
。在替换中,
$1
仅指组1,因此两端的
*
被丢弃。

我成功了!我找到了解决办法。请查看:

<?php
  if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $nota))// looking for special characters
                 {

    $color = 'red';
    $nota = preg_replace("/\*([^*]+)\*/", "<span style=\"color:".$color.";\">$1</span>", $nota );

}

您确定您的
$words
包含一个
*
?为什么要爆炸它?@WiktorStribiżew我需要一些帮助,因为我不知道如何解决这个问题。我是个新手。你能帮我一下吗?你的意见是什么?“我建议的,请考虑投票/接受答案。”
$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);
<?php
  if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $nota))// looking for special characters
                 {

    $color = 'red';
    $nota = preg_replace("/\*([^*]+)\*/", "<span style=\"color:".$color.";\">$1</span>", $nota );

}