user2244804
user2244804

Reputation:

Change color of first character of a word in php

I want to change color of First character of each word in php like

Example Stack

here i want to change color of 'E' of example and 'S' of Stack to BLUE.

i tried this

foreach($chars as $char)
$regexp .= $char . '[a-z0-9]+ ';
$regexp = '^' . rtrim($regexp, ' ') . '$'; 
$req = "SELECT loc_name "
."FROM table "
." WHERE loc_name REGEXP '$regexp'"; 
$query = mysql_query($req);
while($row = mysql_fetch_array($query))
{
$results[] = array('label' => $row['loc_name']);
}

Upvotes: 0

Views: 3271

Answers (1)

revoua
revoua

Reputation: 2059

<?php
$text = 'Example Stack';
$text = preg_replace('/(\b[a-z])/i','<span style="color:blue;">\1</span>',$text);
echo $text;
?>

phpfiddle

Upvotes: 2

Related Questions