maxkiesler Posted April 15, 2007 Share Posted April 15, 2007 Hi, I’m having a rather tough time figuring out one simple thing. How to knock the username: off of a string for display. A normal string looks like: Steve Smith: is going out to see a fight with the guys. What I’m trying to get rid of is Steve Smith: and leave everything else. I tried the following: $string = Steve Smith: is going out to see a fight with the guys.; echo preg_replace("/.*:/", "", $string); This returns: is going out to see a fight with the guys. This works fine if there are no more colons in the sentence. This does not work Steve Smith: is going out to: see a fight with the guys. Has this return: see a fight with the guys. Thanks in advance for any help. Link to comment https://forums.phpfreaks.com/topic/47059-trying-to-preg_replace-one-instance-of-a-colon/ Share on other sites More sharing options...
Guest prozente Posted April 15, 2007 Share Posted April 15, 2007 $string = 'Steve Smith: is going out to: see a fight with the guys.'; echo preg_replace('/(?:[^:]*).*)/', '\1', $string); Link to comment https://forums.phpfreaks.com/topic/47059-trying-to-preg_replace-one-instance-of-a-colon/#findComment-229531 Share on other sites More sharing options...
maxkiesler Posted April 15, 2007 Author Share Posted April 15, 2007 Thanks prozente, your solution worked perfectly. Cheers, Max Link to comment https://forums.phpfreaks.com/topic/47059-trying-to-preg_replace-one-instance-of-a-colon/#findComment-229540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.