views:

78

answers:

4

i have a sql table full of address' like this

Hattie:07903 000066::Dad:07854 000095::Tom:07903 000044::Allistair:07765 000005::Home:0115 974 0000::

i would like to split each and every one so that the name and number of each contact is echoed into a seprate panel within a listbox like below

<ol>
<li>Hattie:07903 000066</li>
<li>Dad:07854 000095</li>
</ol> 

Any help would be brilliant.

Thanks

Mason

+3  A: 

You can split your string like this:

$adresses = explode("::", $yourString)

And then a simple foreach will print the content as you wish.

Colin Hebert
ahh i feel like such a noob XD sorry this was a redicoulous question
Could you show me the foreach i would use please
You can see the complete code in @Mark Baker solution. But it would be better for you to understand the code before copy/pasting it.
Colin Hebert
@Colin - remember that split() is now deprecated as of 5.3.0
Mark Baker
@Mark Baker, you're right, I'm kind of old fashioned. I'll correct that.
Colin Hebert
@Colin - it's the type of thing I'm always forgetting in my own code when I upgrade PHP versions as well
Mark Baker
+2  A: 

It would be better to restructure your database so that addresses aren't stored as a delimited string, but with each address as a row in an addresses table... preferably with the name and address as separate columns... makes it a lot harder for searching, and for manipulation of the data (adding a new address, etc)

$addresses = explode('::',$addressField);
echo '<ol>';
foreach($addresses as $address) {
   echo '<li>',$address,'</li>';
}
echo '</ol>';

EDIT

(Shamelessly stealing formatting from Prix's answer)

$addresses = explode('::',$addressField);
echo '<ol>';
foreach($addresses as $address) {
   list($name,$phone) = explode(':',$address);
   echo '<li>Name: ',$name,' Telephone: ',$phone,'</li>';
}
echo '</ol>';
Mark Baker
If you are lifting code from other response, you could at least give it a credit - +1 it.
Dummy00001
@Dummy00001 - Lifting formatting of the output rather than lifting code... and SO is complaining that I've already upvoted it, so I can't do it again
Mark Baker
With that code complexity it is hard to tell formatting from real code ;)
Dummy00001
@Dummy - admitted it's pretty trivial, and the similarities between Perl and PHP are quite striking in something that simple... I'm still trying to work out why SO doesn't seem to have registered my upvote correctly... I can't see my vote against Prix's answer, but it won't let me vote again because it says I have already voted
Mark Baker
+2  A: 
#!/usr/bin/perl

use warnings;
use strict;

my $input = "Hattie:07903 000066::Dad:07854 000095::Tom:07903 000044::Allistair:07765 000005::Home:0115 9740000::";
my @output = map { split(/::/, $_) } $input;

print "<ol>\n";
foreach my $var (@output) {
    print "<li>$var</li>\n";
}

# or

foreach my $var (@output) {
    my ($name,$number) = split(/:/, $var);
    print "<li>Name: $name\t Number: $number</li>\n";
}
print "</ol>\n";

The output would be:

<li>Hattie:07903 000066</li>
<li>Dad:07854 000095</li>
<li>Tom:07903 000044</li>
<li>Allistair:07765 000005</li>
<li>Home:0115 974 0000</li>

Or:

<li>Name: Hattie Telephone: 07903 000066</li>
<li>Name: Dad Telephone: 07854 000095</li>
<li>Name: Tom Telephone: 07903 000044</li>
<li>Name: Allistair Telephone: 07765 000005</li>
<li>Name: Home Telephone: 0115 974 0000</li>
Prix
+4  A: 

No sweat in Perl:

#! /usr/bin/perl

local $_ = "Hattie:07903 000066::Dad:07854 000095::Tom:07903 000044::Allistair:07765 000005::Home:0115 974 0000::";

print "<ol>\n",
      map("<li>$_</li>\n", split /::/),
      "</ol>\n";

As used in your question, the sequence :: is a terminator and not a separator, which makes split a bit of a conceptual mismatch. To fit the format more closely, use a regular-expression match in list context with /g

The /g modifier specifies global pattern matching—that is, matching as many times as possible within the string. How it behaves depends on the context. In list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression.

and a non-greedy quantifier

By default, a quantified subpattern is “greedy,” that is, it will match as many times as possible (given a particular starting location) while still allowing the rest of the pattern to match. If you want it to match the minimum number of times possible, follow the quantifier with a ?. Note that the meanings don't change, just the “greediness” …

That amounts to

print "<ol>\n",
      map("<li>$_</li>\n", /(.+?)::/g),
      "</ol>\n";

Either way, the output is

<ol>
<li>Hattie:07903 000066</li>
<li>Dad:07854 000095</li>
<li>Tom:07903 000044</li>
<li>Allistair:07765 000005</li>
<li>Home:0115 974 0000</li>
</ol>
Greg Bacon