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??
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.
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:
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: