formating text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

formating text

Post 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>".
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

[php_man]str_replace[/php_man]
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

what would the code look like for str_replace to find the begging and end of a line
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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');
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

$words = explode(" - ", $data);
echo "<tr>
    <td>{$words[0]}</td>
    <td>{$words[1]}</td>
</tr>
";
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

Code: Select all

<?php
echo '<center>';
$title = $_POST&#1111;'section'];
echo $title;
echo '<table border=1 width=55% cellpadding=0 cellspacing=0 bordercolor=black>';

$content = $_POST&#1111;'list'];

$lines = explode("\r\n", $_POST&#1111;'list']); 
foreach ($lines as $line) &#123;
    $words = explode('–', $line);
    echo '<tr><td>'.$words&#1111;0].'</td><td>'.$words&#1111;1].'</td></tr>';
&#125;

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?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

check if the line is empty before exploding it and outputing <td>

look at trim()
Post Reply