Link to home
Start Free TrialLog in
Avatar of Zado
ZadoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Remove elements of string

Hi,


I have a string like so:
2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-2010-3950(32)-4477(37)
And want to remove repeating year numbers from the string, so final output for string above would be:
2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-3950(32)-4477(37)

So I need a script doing the following work:
- check if there's repeated year numbers (format=[four_digits][hyphen]);
- if so - remove second year number and leave the first one (as in example string above);


Thanks for any help.
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

This should do it:
<?php
$string = "2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-2010-3950(32)-4477(37)";

$array = explode("-",$string);

$data = array();
foreach($array as $key => $value){
	if(!in_array($value,$data)){
		$data[] = $value;
	}
}

$string = implode("-",$data);

echo $string;
?>

Open in new window

SOLUTION
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zado

ASKER

Thanks for your help guys, I'm just checking your scripts.
Thanks Ray, I also thought about create separate columns for each year in mysql database, would be much better to maintain the data I think.
Yes, I would think that might be one way to do it.  Or you could have a table with (at least) two columns and as many rows as needed.  One column would be the DATETIME (which could contain the year, as well as more precise timestamp information) and the other column(s) could contain the associated data elements.
Avatar of Zado

ASKER

Thanks for advice. One more thing: can I use your script to affect all rows in database? I mean this:
mysql_connect("localhost","admin","*******");
@mysql_select_db("pcpm") or die( "Unable to connect with database");
$result = mysql_query("SELECT * FROM catalogue_data");
while ($row=mysql_fetch_assoc($result))
{
    $str=($row['db_column']);
    // your script here
}

Open in new window

I have some problems when I use your script this way, or maybe I do something wrong.
Which script
Show us the CREATE TABLE statement.  I am sure we can show you some good ways to get the data out.
Avatar of Zado

ASKER

Ray's one.
galexander07, I found one problem in yours: it removes also repeated regular numbers from string - numbers with brackets, not years. Well, that would make script even better, but yours removes same number in different years, example of 'before' and 'after' below:

2009-1178(10)-1323(11)-2010-2186(18)-1323(11)-2596(21)
2009-1178(10)-1323(11)-2010-2186(18)-2596(21)
Avatar of Zado

ASKER

I fixes it, just cleaned $year variable at the end or the script:
mysql_connect("localhost","admin","*******");
@mysql_select_db("pcpm") or die( "Unable to connect with database");
$result = mysql_query("SELECT * FROM catalogue_data");
while ($row=mysql_fetch_assoc($result))
{
    $str=($row['db_column']);
    // your script here
    $year='';
}

Open in new window

Thanks a lot for your help.