Home  >  Article  >  Backend Development  >  How to change the first character color in php

How to change the first character color in php

藏色散人
藏色散人Original
2018-12-22 14:28:194162browse


PHP更改字符串中第一个字符的颜色,我们可以通过preg_replace()函数来实现,也就是正则替换的方法。

How to change the first character color in php

下面我们就结合简单的代码示例,给大家介绍PHP更改字符串首个字符颜色的方法。

代码示例如下:

<?php
$text = &#39;PHP Tutorial&#39;;
$text = preg_replace(&#39;/(\b[a-z])/i&#39;,&#39;<span style="color:red;">\1</span>&#39;,$text);
echo $text;

效果如下图所示:

How to change the first character color in php

preg_replace 表示 执行一个正则表达式的搜索和替换。

其语法:

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

搜索subject中匹配pattern的部分, 以replacement进行替换。如果subject参数是数组,则函数返回数组,否则返回字符串。如果找到匹配项,将返回新主题,否则主题将保持不变,如果发生错误则返回NULL。

:PHP 5.5.0 起, 传入 "\e" 修饰符的时候,会产生一个 E_DEPRECATED 错误; PHP 7.0.0 起,会产生 E_WARNING 错误,同时 "\e" 也无法起效。

本篇文章就是关于PHP更改字符串首个字符颜色的方法介绍,非常简单易懂,希望对需要的朋友有所帮助!


The above is the detailed content of How to change the first character color in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What are global variablesNext article:What are global variables