Solved

How to remove string elements in php?

Posted on 2012-04-02
8
236 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 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 110

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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 110

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

688 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