Closed Thread Icon

Topic awaiting preservation: regex help Pages that link to <a href="https://ozoneasylum.com/backlink?for=21991" title="Pages that link to Topic awaiting preservation: regex help" rel="nofollow" >Topic awaiting preservation: regex help\

 
Author Thread
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 05-30-2004 18:18

Greets!

I have data in a MySQL table cell that might have data that looks like this:
a:1:{i:2;s:15:"Bill Smith";}
I need to convert echo it out as
<a href="/link/to/user.php?id=2">Bill Smith</a>

where "id" is the value of "i" in the original data.

The problem gets harder, because that same table might hold something like
a:4:{i:2;s:12:"Bill Smith";i:16;s:15:"John Doe";i:77;s:10:"Jack Jones";i:27;s:10:"Ed Johnson";}

And I need to create
<a href="/link/to/user.php?id=2">Bill Smith</a>, <a href="/link/to/user.php?id=16">John Doe</a>, <a href="/link/to/user.php?id=77">Jack Jones</a>, <a href="/link/to/user.php?id=27">Ed Johnson</a>

You'll note that the value of "a" in each shows the total number of users in the array. The value of "s" isn't needed.

This is making my brain hurt. Ideas?



(Edited by Pugzly on 05-30-2004 18:18)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 05-30-2004 18:23

php->unserialize
is probably what you *should* be using.

otherwise, I wouldn't do this with regexps, but with a simple byte-by-byte parser -easy enough to build, and unlike regexps it will descend to any depth (limited by memomry of course)

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 05-30-2004 23:57

Ah - never saw that before. That would explain the initial format.

Thanks. I'll dive into that.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 05-31-2004 06:40

$row['touserarray'] = "a:4:{i:2;s:12:"Bill Smith";i:16;s:15:"John Doe";i:77;s:10:"Jack Jones";i:27;s:10:"Ed Johnson";}";

$tousers = unserialize($row['touserarray']);
foreach ($tousers as $value) {
echo $value . ", ";
}

I end up with "Bill Smith, John Doe, Jack Jones, Ed Johnson,"

That's not bad, but I'm unsure how to get the value of i for each. Is that possible?

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 05-31-2004 09:57

well... I'm not sure, those might be the keys,
try

code:
foreach($trousers as $aKey => $value)
{
...
}

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 05-31-2004 22:34

That did the trick.

Thanks!

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-01-2004 09:55

you could also have used php->var_dump on it to see everything in that array.

« BackwardsOnwards »

Show Forum Drop Down Menu