Alex Rivera | Logout

PHP: Replace all instances

Asked 2011-09-04T02:26:04.460
11

I have a chatlog file that looks like this (name represents screenames and text is their chat string)

name: some text
name2: more text
name: text
name3: text

I want to color all names up to the : red.
For example: <font color=red>myname:</fontcolor> hello How would I do this?

I'm not sure why, but this code colors everything after the colon

echo preg_replace('/(.*?):/', "<font color=#F00>$1</font>:", $output);
Edit
Report

2 Answers

0

put the : inside the font tag after $1

echo preg_replace('/^(.*?):/', "<font color=#F00>$1:</font>", $output);
answered 2011-09-04T02:31:12.067
0

Make the regex more specific:

= preg_replace('/^(\w+):/m', ...

Or if usernames can contain non-alphanum symbols:

= preg_replace('/^(\S+):/m', "<b>$1</b>:", $output);
answered 2011-09-04T03:34:45.090

Your Answer