Solved

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

Posted on 2007-03-23
7
490 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is this error or Notice in php error log? 6 32
WordPress TK Title 8 25
PHP - AJAX and MySQL it works only if the value is a number 12 36
test if query has no results 2 19
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 …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

15 Experts available now in Live!

Get 1:1 Help Now