• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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.
0
Zado
Asked:
Zado
  • 4
  • 3
  • 3
2 Solutions
 
Greg AlexanderLead DeveloperCommented:
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

0
 
Greg AlexanderLead DeveloperCommented:
Here is the script functionized :)
<?php
$string = "2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-2010-3950(32)-4477(37)";
$string2 = "2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-1323(11)-2010-2010-3950(32)-4477(37)";


function cool_function($string){
	$array = explode("-",$string);
	
	$data = array();
	foreach($array as $key => $value){
		if(!in_array($value,$data)){
			$data[] = $value;
		}
	}
	
	$string = implode("-",$data);
	
	return $string;
}


echo cool_function($string)."<br>";
echo cool_function($string2);

?>

Open in new window

0
 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_zado.php

Outputs:
string(63) "2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-3950(32)-4477(37)"
string(63) "2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-3950(32)-4477(37)"

I still think you would save yourself a lot of time if you used XML for this data.

All the best, ~Ray
<?php // RAY_temp_zado.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$str = '2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-2010-3950(32)-4477(37)';

// DESIRED OUTPUT FROM THE POST AT EE
$iff = '2009-1178(10)-1323(11)-2010-2186(18)-2596(21)-3950(32)-4477(37)';

// EXPLODE ON HYPHENS
$arr = explode('-', $str);
$new = array();

// ITERATE OVER THE ARRAY
foreach ($arr as $thing)
{
    // IF THIS IS A RECOGNIZABLE YEAR
    if (strtotime($thing))
    {
        // MAKE EACH YEAR A PLACEHOLDER IN THE ARRAY
        $year = $thing;
        if (!isset($new[$year]))
        {
            $new[$year] = NULL;
            continue;
        }
    }
    else
    {
        // APPEND THE DATA TO THIS YEAR'S VALUES
        $new[$year] .= '-' . $thing;
    }
}

// ACTIVATE THIS TO SEE HOW WE BUILT THE DATA
// var_dump($str);
// print_r($new);


// RECONSTRUCT THE STRING USING THE YEAR KEYS AND THE STRING VALUES
$out = NULL;
foreach ($new as $year => $data)
{
    $out .= '-' . $year . $data;
}
$out = ltrim($out, '-');

// SHOW THE WORK PRODUCT AND THE DESIRED PRODUCT TO SEE THEY ARE THE SAME
var_dump($out);
var_dump($iff);

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
ZadoAuthor Commented:
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.
0
 
Ray PaseurCommented:
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.
0
 
ZadoAuthor Commented:
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.
0
 
Greg AlexanderLead DeveloperCommented:
Which script
0
 
Ray PaseurCommented:
Show us the CREATE TABLE statement.  I am sure we can show you some good ways to get the data out.
0
 
ZadoAuthor Commented:
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)
0
 
ZadoAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now