Open In App

How to change the color of the first character of a word using PHP script?

The preg_replace() function is an in-built function that returns a word or array of words with the replacement of some content after searching the pattern. If the search pattern is not found then it will be returned unchanged. 

Approach:



Example 1:




<?php
  
// Text to replace
$text = "Geeks For Geeks";
  
// The preg_replace is used here to replace the 
// color of first character of the word
$text = preg_replace('/(\b[a-z])/i'
'<span style="color:green;">\1</span>', $text);
  
// Display the text value
echo $text
?>

Output:



Example 2:




<?php
  
// Text to replace
$text = "Practice code in GEEKSFORGEEKS";
  
// The preg_replace is used here to replace the 
// color of first character of the word
$text = preg_replace('/(\b[a-z])/i'
'<span style="color:green;">\1</span>', $text);
  
// Display the text value
echo $text
?>

Output:

Article Tags :