NYCPHP Meetup

NYPHP.org

[nycphp-talk] string function

Brian D. brian at realm3.com
Fri Jul 4 07:38:18 EDT 2008


Note: This response is more or less tongue in cheek, and is not meant
to be taken seriously.

<?php
$y="tnk81|98.8$|yuhj78t|32.6$|tris78y|459.78$|";

$y_len = strlen($y);

$results = array();
$in_name = true;

$name = '';
$value = '';

for ($i=0; $i <= $y_len; $i++) {
    if ($y[$i] == '|') {
        if (!$in_name) {
            if ($name) {
                $results[$name] = $value;
                $name = '';
                $value = '';
            }
        }
        $in_name = !$in_name;
    } else {
        if ($in_name) {
            $name .= $y[$i];
        } else {
            $value .= $y[$i];
        }
    }
}
var_dump($results);

===

array(3) {
  ["tnk81"]=>
  string(5) "98.8$"
  ["yuhj78t"]=>
  string(5) "32.6$"
  ["tris78y"]=>
  string(7) "459.78$"
}


On Wed, Jul 2, 2008 at 5:35 PM, chad qian <nynj.tech at hotmail.com> wrote:
> An unknown string,its format is:
> y="tnk81|98.8$|yuhj78t|32.6$|tris78y|459.78$|....."
>
> I need output format:
> tnk81      98.8$
> yuhj78t   32.6$
> tris78y    459.78$
> .........................
>
> How to program php to get this output?String is divided by "|".
>
> Thanks!
>
> chad
>
> ________________________________
> Need to know now? Get instant answers with Windows Live Messenger. IM on
> your terms.
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>



-- 
realm3 web applications [realm3.com]
Information architecture, application development.
phone: (917) 512-3594
fax: (440) 744-3559



More information about the talk mailing list