Page 1 of 1
formating text
Posted: Thu Dec 02, 2004 6:03 pm
by robjime
I have some text that comes in this format:
word - word
I want to make it into a table like
Code: Select all
<tr>
<td>word</td>
<td>word</td>
</tr>
how can i detect the begining and end of each line? I know I can find "-" and put "</td><td>".
Posted: Thu Dec 02, 2004 6:11 pm
by John Cartwright
[php_man]str_replace[/php_man]
Posted: Thu Dec 02, 2004 6:17 pm
by robjime
what would the code look like for str_replace to find the begging and end of a line
Posted: Thu Dec 02, 2004 8:55 pm
by rehfeld
Code: Select all
$new_line = "\r\n"; // must use double quotes
$data = '
word1
word2
word3
';
// to seperate each line into an array
$lines = explode("\r\n", $data);
// if the data is in a file, you can just do this
$lines = file('foo.txt');
Posted: Thu Dec 02, 2004 10:04 pm
by Christopher
Code: Select all
$words = explode(" - ", $data);
echo "<tr>
<td>{$words[0]}</td>
<td>{$words[1]}</td>
</tr>
";
Posted: Sat Dec 04, 2004 8:31 am
by robjime
i couldn't get any of these methods to work on detecting the end of a line, but detecting the dash worked perfectly
Posted: Sat Dec 04, 2004 9:16 am
by timvw
How do you define end of line? Usually people assume it is \n.
Anyway, detecting the position of something in a string, can be done with
http://www.php.net/strpos
Posted: Sat Dec 04, 2004 2:53 pm
by robjime
Code: Select all
echo '<center>';
echo '<table border=1 width=55%>';
$title = $_POST['section'];
$content = $_POST['list'];
$lines = explode("\r\n", $_POST['list']);
$words = explode("–", $lines);
$i = 0;
$pnum = count($words);
$num = $pnum * 2;
while($i<$num){
echo '<tr>';
echo "
<td>{$words[$i]}</td>
<td>{$words[$i+1]}</td>
";
echo '</tr>';
$i++;
}
echo '</table>';
echo '</center>';
?>
this is what i have so far but it returns array.
Posted: Sat Dec 04, 2004 4:03 pm
by rehfeld
did the code i gave you on codewalkers not work?
your code is not working because your trying to explode an array, you cant do that
$words = explode("–", $lines); // lines in an array
you need to use explode on 1 line at a time
Posted: Sat Dec 04, 2004 9:14 pm
by dull1554
try this
Code: Select all
for($k = 1;$k<count($lines);$k++) {
$words[$k] = explode("-", $lines);
}
this will return a multi dimensional array
line one would be broken up into $words[1][0],$words[1][1],$words[1][2],$words[1][3],$words[1][4],and so on and so fourth..
then you can make another loop with a couple of vars to cycle through the multi-dimensional array and print out what ever you want
Posted: Tue Jan 11, 2005 11:00 am
by robjime
Code: Select all
<?php
echo '<center>';
$title = $_POSTї'section'];
echo $title;
echo '<table border=1 width=55% cellpadding=0 cellspacing=0 bordercolor=black>';
$content = $_POSTї'list'];
$lines = explode("\r\n", $_POSTї'list']);
foreach ($lines as $line) {
$words = explode('–', $line);
echo '<tr><td>'.$wordsї0].'</td><td>'.$wordsї1].'</td></tr>';
}
echo '</table>';
echo '</center>';
?>
--------------------------------------------------------------------------------
this works but its giving an extra <tr><td></td></tr> after every 2 words. So its like:
<tr><td>Word1</td><td>word2</td></tr>
<tr><td></td><td></td></tr>
<tr><td>word3</td><td>word4</td></tr>!??!?!?
Can i eliminate the extra line?
Posted: Tue Jan 11, 2005 1:18 pm
by rehfeld
check if the line is empty before exploding it and outputing <td>
look at trim()