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
Solved

How to remove string elements in php?

Posted on 2012-04-02
8
233 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…

828 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