Jump to content

[SOLVED] Modifying large strings or files.


DeepakJ

Recommended Posts

Ok I need to explode the following file around | but the problem is that there is a huge space between the first line and the second. Manually changing the file is not an option considering there are about 2000 of these files. I was wondering if someone could tell me how to get rid of the large space. This is how my file looks like:

Service Pack 2 for VisualMill 5 has been released!
0|Please go to Downloads->Service Packs and download the Service Pack 2 for VisualMill. <br><br>You can also get the complete product with Service Pack included from the Support->Download 5.0 link.<br><br>The bugs fixed in this Service Pack are as follows:<br><br>1. Added rotate by 3 points to command to Edit menu <br>2. Added Angle command to Measure toolbar <br>3. Changed precision of measure output <br>4. Saving/Restoring maximum simulation settings from browser <br>5. Allowing animation if stock model is not loaded <br>6. V-Cutting bug fixed <br>7. Fixed Climb/Conventional bug in pocketing, horizontal optimized finishing <br>8. Angle is input as radians rather than degrees for command line version of rotate <br>9. Thread tool simulation error fixed <br>10. Profile number of cuts not transferred from V4 to V5 fixed <br>11. Extract surface edges now extracts the closest edge to the pick point rather than all of the edges <br>12. A Rhino surface importing problem fixed <br>13. Rotated 21/2 Axis was not working correctly <br>14. Introducing 4Axis add-on for VisualMill Basic <br>15. Resource changes for multi language support <br>16. Advanced cut levels fixes for multi-language support|1073425551|MecSoft Support|199.174.76.62

Link to comment
Share on other sites

When i explode that file it only comes up with the first line. If I get rid of that line then explode works fine. I can attach the file if it makes more sense. This program is basically a forum converter and posts from the old forum are in that format. I need to change the format to queries which can be put into mysql. I basically have a PHP script echo the code using fopen and then echoing exploded parts of the script into a query.

Link to comment
Share on other sites

Try:

<?php

$lines = file('file.txt');

$k = 0;
$new_lines = array();

for($i = 0; $i < count($lines)/2; $i++)
{
    $new_lines[] = trim($lines[$k]) . '|' . $lines[$k+1];

    $k = $i+2;
}

foreach($new_lines as $line)
{
    list($header, $id, $content, $timestamp, $author, $ip) = explode('|', $line);

    $date = date('D jS M Y', $timestamp);
    $id++;
    $content = nl2br($content);

    echo <<<EOF
<h1>$header</h1>
<p>#$id - Posted by: $author ($ip) at $date</p>
<p>$content</p>
<hr />
EOF;
}

?>

I'm asumming your file goes in this format:

Heading
[post-id]|[content for post]|[timestamp]|[Author]|[iP Address of Author]
Heading
[post-id]|[content for post]|[timestamp]|[Author]|[iP Address of Author]
Heading
[post-id]|[content for post]|[timestamp]|[Author]|[iP Address of Author]

Link to comment
Share on other sites

Yeah Im replying to you. Im saying that the post order in threads I beleive is based on timestamp rather than the id in phpbb forums, hence there useless ness. Ive been trying another approach but get this error. Im not too exp with fopen so some troubleshooting help would be appreciaited.

 

 

<b>Fatal error</b>:  Cannot break/continue 1 level in <b>C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Forums\index1.php</b> on line <b>37</b><br />

Link to comment
Share on other sites

Try:

<?php

$lines = file('file.txt');

$k = 0;
$new_lines = array();

for($i = 0; $i < count($lines)/2; $i++)
{
    $new_line = trim($lines[$k+1]);

    $col = explode('|', $new_line);

    $id = $col[0];
    $timestamp = $col[2];

    $new_line = str_replace(array($id . '|', $timestamp . '|'), '', $new_line);

    $new_lines[$timestamp] = trim($lines[$k]) . '|' . $new_line;

    $k += 2;
}

ksort($new_lines);

foreach($new_lines as $k => $line)
{
    list($header, $content, $author, $ip) = explode('|', trim($line));

    $date = date('D jS M Y', $k);
    $id++;
    $content = str_replace('<br>', "<br />\n  ", $content);

    echo "<h1>$header</h1>
<p>Posted by: $author ($ip) at $date</p>
<p>
  $content
</p>\n
<hr />\n";
}

?>

I have removed the post id and the posts should display in ascending order (earliest (top) to recent (bottom))

Link to comment
Share on other sites

Im getting these errors. Thanks alot for your time nad effort :D

 

Notice: Undefined offset: 1 in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Forums\file.php on line 10

 

Notice: Undefined offset: 2 in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Forums\file.php on line 15

 

Warning: date() expects parameter 2 to be long, string given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Forums\file.php on line 30

Link to comment
Share on other sites

Have you modified the file.txt's format. Make sure its in this format:

[post title]
[post id]|[content for post]|[timstamp]|[author]|[ip address]

 

Looks like you have changed that file and the code has broken due to the changes you have made to the file

 

The result of running the script will be this

Link to comment
Share on other sites

Strict Standards: date() [function.date]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for '-7.0/DST' instead in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Forums\file.php on line 30

 

Now I am getting this error

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.