i need a couple of line of code in php

Discussion in 'PHP' started by pizzaman, Dec 12, 2007.

  1. #1
    hello
    i am trying to do something like this in php

    i have a variable in my db that is formated like this
    $myvariable=a4.500012b74.35666|a5.7545b5.3261|a54.32b36.221
    the string will have diffrent lenght at diffrent times in another word i do not know how many pairs of info iare there
    i want to break this up and have a list of the pairs in xml
    <point a="4.500012" b="74.35666" />
    <point a="5.7545" b="5.3261" />
    <point a="54.32" b="36.221" />
     
    PHP:
     
    pizzaman, Dec 12, 2007 IP
    iggysick likes this.
  2. vonvhen

    vonvhen Peon

    Messages:
    152
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try using the 'explode' function for '|'. This will put your points in an array.
    You will then can loop through your array and truncate your string.
     
    vonvhen, Dec 12, 2007 IP
  3. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Slightly more than a couple of lines...

    
    $myvariable="a4.500012b74.35666|a5.7545b5.3261|a54.32b36.221";
    $exp = explode("|",$myvariable);
    foreach ($exp as $var) {
    	$out = explode("b",$var);
    	$output_1 = str_replace("a","",$out[0]);
    	$output_2 = $out[1];
    	echo "<point a=\"" . $output_1 . "\" b=\"" . $output_2 . "\" />\n";
    }
    
    PHP:
     
    Gawk, Dec 12, 2007 IP
    pizzaman likes this.
  4. pizzaman

    pizzaman Active Member

    Messages:
    4,053
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    90
    #4
    thanks guys
     
    pizzaman, Dec 12, 2007 IP
    guerilla and iggysick like this.
  5. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    LOL.
    use
    $output_1 = substr($out[0],1);
    instead of
    $output_1 = str_replace("a","",$out[0]);
     
    codesome, Dec 12, 2007 IP
  6. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #6
    heh heh - when tired take a break ;)
     
    Gawk, Dec 12, 2007 IP