?
Solved

How to remove string elements in php?

Posted on 2012-04-02
8
Medium Priority
?
242 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
[X]
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
  • 3
  • 3
  • 2
8 Comments
 
LVL 15

Accepted Solution

by:
StingRaY earned 2000 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 111

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
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!

 
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 111

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

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.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
The viewer will learn how to dynamically set the form action using jQuery.
Progress
Suggested Courses

649 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