Solved

How to remove string elements in php?

Posted on 2012-04-02
8
232 Views
Last Modified: 2012-04-02
Hi Experts,

I've got two strings:
$str1 = '3,4,5,6,7,8,9';
$str2 = '5,6,7,8';
Now, I want to remove all $str2 string elements from $str1 string, something like $str1-$str2, so a result would be '3,4,9'. I'm looking for some quick and easy way.

Thanks.
0
Comment
Question by:Zado
  • 3
  • 3
  • 2
8 Comments
 
LVL 15

Accepted Solution

by:
StingRaY earned 500 total points
ID: 37796145
Try this:

<?php

$str1 = '3,4,5,6,7,8,9';
$str2 = '5,6,7,8';

$str3 = implode(',',array_diff(explode(',',$str1),explode(',',$str2)));
echo $str3;
?>

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 37796246
Nice one, thanks :-)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37796266
Are these two strings high-quality test data, representative of your exact data set?  If so, array_diff() is the right tool.  If the data is a little bit more complicated in its construction or is delimited differently (not all trimmed strings separated by commas) there might be a little more to the process.  The quality of the answers you get at EE will depend on the quality of the test data you give us at EE.  Just wondering... ~Ray
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 8

Author Comment

by:Zado
ID: 37797256
Thanks Ray and sorry, but I accepted the answer before you posted here. It's pretty simple data there, the numbers are used as examples here, there could be anything, but it always be separated by comma. Thanks for your comment anyway, all the best!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37797379
Yeah, was wondering about something like this...
$str1 = '3, 4, 5, 6,7,8';
$str2 = '3,4 , 5';
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37797462
@Ray_Paseur

Just use preg_split instead of explode.
0
 
LVL 8

Author Comment

by:Zado
ID: 37797494
Good to know, thanks for that.
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37797537
Both answers $str3 and $str6 are identical, 3,4,9.

<?php

$str1 = '3,4,5,6,7,8,9';
$str2 = '5,6,7,8';

$str3 = implode(',',array_diff(explode(',',$str1),explode(',',$str2)));
echo $str3;
echo "\n";

$str4 = '3,4, 5,6, 7,8 ,9';
$str5 = '5, 6,7 ,8';

$str6 = implode(',',array_diff(preg_split('/\s*,\s*/',$str4),preg_split('/\s*,\s*/',$str5)));

echo $str6;
?>

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

813 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

14 Experts available now in Live!

Get 1:1 Help Now