simple preg_replace question - remove everything after the comma - inc. the comma

Posted on 2007-03-23
Last Modified: 2013-12-13
I have a database field "$row_Recordset1['Employee']" that contains lastname, firstname. I want to strip out everything after the , (including the ,) so that it only displays lastname. Im having difficulty getting preg_replace to do what i want. its still displaying the comma

$lastname = preg_replace('/^([^ ]+).*/','\1', $row_Recordset1['Employee']);

any suggestions? Thanks.
Question by:nriddock
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 18780040
Hello nriddock ,


$lastname = substr($row_Recordset1['Employee'], 0, strpos($row_Recordset1['Employee']), ',')-1);  



Author Comment

ID: 18780071
Parse error: parse error in /var/www/html/php/project_status.php on line 135
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 18780095

sorry should be

$lastname = substr($row_Recordset1['Employee'], 0, strpos($row_Recordset1['Employee'], ',')-1);  
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 18780128
comma is gone...but now its stripping off the last letter of their name
LVL 22

Accepted Solution

Ivo Stoykov earned 500 total points
ID: 18780227

OK  nriddock

please try this

$lastname = substr($row_Recordset1['Employee'], 0, strpos($row_Recordset1['Employee'], ','));  

Author Comment

ID: 18780246
that worked!!!

thanks for all your help.
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 18780256
welcome and good luck ;-)


Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question