Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PHP3] Regular expressions... 8/

0 views
Skip to first unread message

Martin Edelius

unread,
Mar 13, 2000, 3:00:00 AM3/13/00
to
Hi.

Sorry to be using this list for this kind of questions but I need some
expert help on this one so...

I have a string of numbers separated with comma (,). Like so:
1,2,3,4,5,12,55,72,182,211,321

There can't be any duplicates in this string or my script won't work.
Unfortunately this happens for some reason I haven't figured out yet so for
the time being I have to try and remedy the effect and not the cause. 8(

I've been fooling around with regexps as they seem to be the right tool for
the job but I can't for the life of me understand how to get them to do what
I want.

I know how to find a given string with a regexp but not a dynamically
created one. For instance, lets say that my string looks like this:
4,5,211,211,211,211,223,231

In this case I'd like the regexp to find the 211's and replace them with one
instance of 211:
4,5,211,223,231

Should I use a regexp within a regexp for this? What would it look like? I
am getting better at regexps but this seems to be way over my head. I don't
mind using more than one iteration either so if I need a couple of regexps
to do the job that's fine with me.

I'm on a deadline so any and all ideas and suggestions are more than
welcome.

TIA.


Best regards,
Martin Edelius

Spirex Digital Design
--------------------------------
www: http://www.spirex.se
Mail: martin....@spirex.se
Phone: +46-31-514651, 0708-113711
Fax: +46-31-514331
Aröds Industriväg 3c
S-422 43 Hisings Backa
--------------------------------
If I haven't got back to you or done what I'm supposed to, let me know again
as I have too much to do for my own good...

--
PHP 3 Mailing List <http://www.php.net/>
To unsubscribe, send an empty message to php3-uns...@lists.php.net
To subscribe to the digest, e-mail: php3-diges...@lists.php.net
To search the mailing list archive, go to: http://www.php.net/mailsearch.php3
To contact the list administrators, e-mail: php-lis...@lists.php.net


NickM

unread,
Mar 13, 2000, 3:00:00 AM3/13/00
to
Why not just explode the list in to an associative array, any dupes will be
overwritten. Else if you need to know of the dupes then process the array
with a loop.

Nick

Martin Edelius

unread,
Mar 13, 2000, 3:00:00 AM3/13/00
to
If I explode() the string it will only end up with the duplicate entries in
the array as well. Or are you suggesting that I use the exploded array
values as indices in another array that I assign the value of the first,
exploded, array? That might work.

I don't know where the duplicate entries will occur or how many times in a
row so I have to find a way to handle everything automatically. 8/

Thanks for the feedback.


Best regards,
Martin Edelius

Spirex Digital Design
--------------------------------
www: http://www.spirex.se
Mail: martin....@spirex.se
Phone: +46-31-514651, 0708-113711
Fax: +46-31-514331
Aröds Industriväg 3c
S-422 43 Hisings Backa
--------------------------------
If I haven't got back to you or done what I'm supposed to, let me know again
as I have too much to do for my own good...

NickM

unread,
Mar 13, 2000, 3:00:00 AM3/13/00
to
Yes, kind of like this:
$data = array(4,5,211,211,211,211,223,231);
while(list($pos,$val) = each($data)){
$temp[(string)$val] = $pos;
}
while(list($val,$pos) = each($temp)){
print "$val,$pos<br>";
}

might not be the best way but you said you were looking for a quick and
dirty solution.

Martin Edelius

unread,
Mar 14, 2000, 3:00:00 AM3/14/00
to
The code you suggest below only almost works. It assigns the position to the
new array, not the value. This is how I did it:

while(list($pos,$val) = each($data)){
$temp[(string)$val] = $val;

and it worked like a charm. 8)

Thanks a lot for the help.


-- Martin

NickM

unread,
Mar 14, 2000, 3:00:00 AM3/14/00
to
Yeah I kept the position in case you wanted to know what places where
striped, the values, as the print statment shows, are in the other side of
the array. But this depends on how you want to use it, like you say
position is irrelivant to you so I mean you could stick nothing in there if
you wanted.

No problem dude,

0 new messages