Solved

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

Posted on 2007-03-23
7
489 Views
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.
0
Comment
Question by:nriddock
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 18780040
Hello nriddock ,

try

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

HTH

!i!
0
 

Author Comment

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

Expert Comment

by:Ivo Stoykov
ID: 18780095


sorry should be

$lastname = substr($row_Recordset1['Employee'], 0, strpos($row_Recordset1['Employee'], ',')-1);  
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

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

Accepted Solution

by:
Ivo Stoykov earned 500 total points
ID: 18780227

OK  nriddock

please try this

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

Author Comment

by:nriddock
ID: 18780246
that worked!!!

thanks for all your help.
0
 
LVL 22

Expert Comment

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

!i!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now