Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ezkimo

macrumors regular
Original poster
Sep 12, 2002
216
0
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):


Monday, February 4
Orientation for Business Professionals @ 12:00 pm MST
(XXX) XXX-XXXX, access code XXXX-XXXX-XXXX
https://www1.gotomeeting.com/register/XXXXXXXX

Account Training @ 1:00 pm MST
(XXX) XXX-XXXX, access code XXXX-XXXX-XXXX
https://www1.gotomeeting.com/register/XXXXXXXX

Tuesday, February 5
Orientation for Business Professionals @ 12:00 pm MST
(XXX) XXX-XXXX, access code XXXX-XXXX-XXXX
https://www1.gotomeeting.com/register/XXXXXXXX

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!
 
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>';
 
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!
 
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!

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.
 
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!
 
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!

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.
 
Thanks! I'm getting the error:

Warning: fopen(test.txt) [function.fopen]: failed to open stream: Permission denied in (path here) on line 11
can't open file

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.
 
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.
 
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);

?>
 
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);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.