Parse Text File

Discussion in 'Web Design and Development' started by ezkimo, Feb 5, 2008.

  1. ezkimo macrumors regular

    Joined:
    Sep 12, 2002
    #1
    Hi,
    Let me preface this by saying I have no idea how to do this sort of thing. I'm a designer by trade and my coding experience is minimal to non in anything except html, css and minor javascript.

    Anyway, every week a client sends me a list of webinars to update their site with.
    The text they send looks like this (but with real info for the x):


    and so on for the remainder of the week.

    I usually just paste the information into an html file that looks like this:

    HTML:
    <div class="webinardate">
    	<div class="title">Monday, February 4  </div>
    	<div class="webinars">
    		
    	<h2>Orientation for Business Professionals @ 12:00 pm MST</h2>
    	<a href="https://www1.gotomeeting.com/register/XXXXXXXX">Video Link </a>
    	<h3>Audio:</h3>(XXX) XXX-XXXX, access code XXXX-XXXX-XXXX
    
    	<h2>Account Training @ 1:00 pm MST</h2>
    	<a href="https://www1.gotomeeting.com/register/XXXXXXXX">Video Link </a>
    	<h3>Audio:</h3>(XXX) XXX-XXXX, access code XXXX-XXXX-XXXX
    	<br /><br />
    	
    	</div>
    </div>
    
    
    <div class="webinardate">
    	<div class="title">Tuesday, February 5  </div>
    	<div class="webinars">
    		
    	<h2>Orientation for Business Professionals @ 12:00 pm MST</h2>
    	<a href="https://www1.gotomeeting.com/register/XXXXXXXX">Video Link </a>
    	<h3>Audio:</h3>(XXX) XXX-XXXX, access code XXXX-XXXX-XXXX
    
    	<h2>Account Training @ 1:00 pm MST</h2>
    	<a href="https://www1.gotomeeting.com/register/XXXXXXXX">Video Link </a>
    	<h3>Audio:</h3>(XXX) XXX-XXXX, access code XXXX-XXXX-XXXX
    	<br /><br />
    	
    	</div>
    </div>
    
    
    The thing is, there can be any number of entries per day (usually about 8), so it takes a long time to do. Is there any way to paste the raw information I receive into something and have it pop out like the formatted code above? The website is written in PHP (nothing fancy), so if I could just put <?php include ('somefile.php'); ?> or something similar and everything would work it would be ideal.

    Any help that somebody could offer would be a huge help. Even pointing me to a website that I could learn what to do.

    Thanks!
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Why don't you ask a hard question :p

    I may have come up with a PHP solution. I have not actually tested this code in way except me staring at it, but should at least get you started. May take some tweaking and I'll try to help with anything that goes wrong with the code as much as I can. I've tried to comment it a bit to make it more understandable.

    Essentially I'm using regular expressions to try and determine what type of line is being processed, then printing out things appropriately.

    PHP:
    $dayStart true;
    $lines file('file.txt');

    foreach (
    $lines as $line_num => $line) {
      
    // If line starts with the name of the week
      
    if(preg_match('/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/'$line)) {
        
    // if beginning of file
        
    if ($dayStart) {
          print 
    '<div class="webinardate">
      <div class="title">'
    $line .'</div>
      <div class="webinars">'
    "\n\n";
          
    $dayStart false;
        }
        else {
          print 
    '<br /><br />
        
        </div>
    </div>

    <div class="webinardate">
      <div class="title">'
    $line .'</div>
      <div class="webinars">'
    "\n\n";
          }
      }
      
    // If line begins with parentheses
      
    else if (preg_match('/^\(/'$line)) {
        print 
    '<h3>Audio:</h3>'$line ."\n";
      }
      
    // If line starts with http
      
    else if (preg_match('/^http/'$line)) {
        print 
    '<a href="'$line .'">Video Link </a>'"\n";
      }
      
    // If a blank line
      
    else if ($line == '') {
        continue;
      }
      
    // Assuming event name
      
    else {
        print 
    '<h2>'$line .'</h2>'"\n";
      }
    }
    // Close up at end of file
    print '<br /><br />
        
        </div>
    </div>'
    ;
     
  3. ezkimo thread starter macrumors regular

    Joined:
    Sep 12, 2002
    #3
    At first glance it works perfectly! Thanks! I owe you big time. You're amazing. :)

    I did change one thing. The product they have has Money in the first word, and the titles of the webinars are often Money Blah Blah, so I changed this line

    PHP:
    if(preg_match('/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/'$line)) { 
    to:

    PHP:
      if(preg_match('/^(Mond|Tue|Wed|Thu|Fri|Sat|Sun)/'$line)) { 
    Which seems to work. Do you think that would cause any problems?

    Thanks again!
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Yeah that little change is fine. I was just trying to keep it short. Let me know if it does work. I was just working from the top of my head so I'm curious if it really does what I think it does. There may also be simpler ways of doing it, but I've been messing with regular expressions lately so that's why I went this route.
     
  5. ezkimo thread starter macrumors regular

    Joined:
    Sep 12, 2002
    #5
    Excuse me for dragging this back from the dead, but I have another question in the same vein.

    Is it possible to search a file for a specific word (I assume that would be called a string?) and then replace that word with another word whenever it occurs? If its extremely complicated then its probably not worth it, just trying to save some time.
    Thanks!
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    This may do it, but practice on a test file since this code will over write the starting file.
    PHP:
    $lines file('file.txt');
    $newFile='';

    foreach (
    $lines as $line_num => $line) {
      
    preg_replace('/oldWord/g''newWord'$line);
      
    $newFile += $line;
    }
    // Then write file
    $fh fopen('file.txt''w') or die("can't open file");
    fwrite($fh$newFile);
    fclose($fh);
    Also, if this is something you need PHP for you can use the Unix sed command. It's not wonderfully easy to use, but not bad,

    sed 's/oldWord/newWord/g' file.txt

    Of course a number of text editors can do search and replace as well.
     
  7. ezkimo thread starter macrumors regular

    Joined:
    Sep 12, 2002
    #7
    Thanks! I'm getting the error:

    I assumed that was a permissions problem, but I've changed the permissions on the folder and the files to 775 and 777 (one of which I think should be correct?) and it still comes up with the error.

    Oh, and I also got an error about the "/g" in the preg_replace('/Born/g', 'Aloha', $line); line, but I deleted the g...which could be the problem.
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    OK, for fopen part, try writing out to a different file name than the one we're using the get $lines from. The permission error may be because we're trying to access the same file twice in different ways.

    For the /g, it was globally finding on that line in case the word appears more than once, but I'm not sure the preg functions use it. So getting rid of it is fine. Though is you want the find portion to be case insensitive it can place 'i' in there instead.
     
  9. ezkimo thread starter macrumors regular

    Joined:
    Sep 12, 2002
    #9
    Ok, there is no error now, but nothing shows up. Just a blank page.

    The code is as follows:
    PHP:
    <?php

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

    foreach (
    $lines as $line_num => $line) { 
      
    preg_replace('/Born/i''Aloha'$line); 
      
    $newFile += $line

    // Then write file 
    $fh fopen('test2.txt''w') or die("can't open file"); 
    fwrite($fh$newFile); 
    fclose($fh);

    ?>


     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    Doh! I made changes to the two lines inside the foreach loop. I tested this so should be good to go now. That's what I get for trying to think first thing in the morning.

    PHP:
    $lines file('test.txt'); 
    $newFile=''

    foreach (
    $lines as $line_num => $line) { 
      
    $line preg_replace('/Born/i''Aloha'$line); 
      
    $newFile .= $line

    // Then write file 
    $fh fopen('test2.txt''w') or die("can't open file"); 
    fwrite($fh$newFile); 
    fclose($fh); 
     

Share This Page