Solved

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

Posted on 2007-03-23
7
491 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 28
How to count in a table in php 22 35
Why do people dis php? 5 49
comma true 6 33
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 …
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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