Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split a string twice, and display...prob easy, but new to php syntax

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034

hello!

i'm just getting used to php...

here's my string:

$string='TEST|123,TEST|487,TEST|309';

and i'd like the output to be:

Code:
<tr>
<td>TEST</td>
<td>123</td>
</tr>
<tr>
<td>TEST</td>
<td>487</td>
</tr>
<tr>
<td>TEST</td>
<td>309</td>
</tr>

i'm sure it's a simple split, foreach loop, but i'm having a time with the syntax.

thanks in advance!

- g
 
Hi

Code:
[url=http://php.net/foreach/]foreach[/url] ([url=http://php.net/split/]split[/url](",",$string) as $r) {
  [url=http://php.net/echo/]echo[/url] "<tr>\n";
  foreach (split("\|",$r) as $f) echo "<td>$f</td>\n";
  echo "</tr>\n";
}

Feherke.
 
would this work too?
Code:
$find = array("|", ",");
$replace = array ("</td><td>", "</tr><tr>");
$html = "<tr><td>" . str_replace($find, $replace, $text) . "</td></tr>";

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top