Populate an array from a list to get

Discussion in 'PHP' started by 123GoToAndPlay, Feb 12, 2010.

  1. #1
    I have a .txt file with

    
    link[|]21/12/2009[|]text
    linkB[|]10/01/2010[|] txt B
    linkC[|]23/02/2010[|]Txt C
    
    Code (markup):
    I like to get this
    
    <li><a href="link"><span>21/12/2009</span> text</a></li> 
    <li><a href="linkB"><span>10/01/2010</span> txt B</a></li> 
    <li><a href="linkC"><span>23/02/2010</span></a></li> 
    
    Code (markup):
    with the following function
    
    	function getTickerContent(){
    		$open = "content/tickerContent.txt";
    		$filedata = file_get_contents ($open);
    		list($data, $text, $link)  = explode("[|]",$filedata); 	
    .....
    .....
    
    	return $tickerContent;
    	}
    
    PHP:
    Any tips? code suggestions??
     
    123GoToAndPlay, Feb 12, 2010 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    The easiest thing to do is just probably explode twice... once for carriage returns, the second time for the individual line.

    $data = explode("\r", $filedata);
    foreach ($data as $line) {
    	$output[] = explode('[|]', $line);
    }
    PHP:
    ...or along those lines.
     
    digitalpoint, Feb 12, 2010 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tx digitalpoint (sounds familar),

    i have produce this
    
    function getData(){
    		$open = "content/tickerContent.txt";
    		$filedata = file_get_contents($open);
    		$data = explode("\r", $filedata);
    		foreach ($data as $line) {
    			$output[] = explode('[|]', $line);
    		}
    		return $output;
    	}
    
    function getTickerContent(){
    	
    	$tickerContents = getData();
    	$res = '<ul>';
    	foreach ($tickerContents as $tickerContent) {
    		$res .= '<li><a href="'.$tickerContent[2].'"><span>'.$tickerContent[0].'</span>'.$tickerContent[1].'</a></li>';
    	}
    	$res .= '</ul>';
    	return $res;
    	}
    echo getTickerContent();
    
    PHP:
     
    123GoToAndPlay, Feb 12, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    or you can use preg_replace()

    <?php
    
    function getTickerContent(){
    $tickerContent = preg_replace("@(.*)\[\|\](.*)\[\|\](.*)@", "<li><a href=\"$1\"><span>$2</span>$3</a></li>", file_get_contents("content/tickerContent.txt"));
    return $tickerContent;
    }
    
    echo getTickerContent();
    
    ?>
    PHP:
     
    Last edited: Feb 13, 2010
    danx10, Feb 13, 2010 IP