Solved

How to remove string elements in php?

Posted on 2012-04-02
8
228 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
Comment Utility
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
Comment Utility
Nice one, thanks :-)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 8

Author Comment

by:Zado
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
@Ray_Paseur

Just use preg_split instead of explode.
0
 
LVL 8

Author Comment

by:Zado
Comment Utility
Good to know, thanks for that.
0
 
LVL 15

Expert Comment

by:StingRaY
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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