Jump to content

Help with ereg please


magic2goodil

Recommended Posts

I am trying to take this document.

[url=http://www.collegebookx.com/text.txt]http://www.collegebookx.com/text.txt[/url]

and then with ereg and preg statements I am eventually going to pull out just the college names.
but right now it is failing my tests

the live code is here: [url=http://www.collegebookx.com/process.php]http://www.collegebookx.com/process.php[/url]

can you tell me why it might be suddenly messing up at 311?
does anyone have a better suggestion for how to do this thing?

here is the code for process.php:
[code]<?php

$handle = @fopen('text.txt', "r");
if ($handle) {
  while (!feof($handle)) {
      $lines[] = fgets($handle, 2827);
  }
  fclose($handle);
}

$i = 0;

while($i < count($lines)) {

$string = $lines[$i];


ereg(">.*</H2>", $string, $ar);

if(ereg(">.*</A>", $string,  $arr)) {

foreach($arr as $a) {

$pattern = "/><A\sHREF=\"http:\/\/(.+)\/\"\sTARGET=\"_blank\">/";
$replacement = "";
$a = preg_replace($pattern, $replacement, $a);

$a = eregi_replace("[^[a-z][</A>]{1}]", "", $a);

$a = eregi_replace("</A>", "", $a);

$lines[$i] = $a;

}

}

echo $i . "=" . $lines[$i] . "<br>";
$i++;
}

?>
[/code]
Link to comment
Share on other sites

Don't bump your post this soon!

Remember that people viewing this board are based all over the world, mainly in the US, so many of them haven't even seen your post yet.    I think you should wait at least 4 hours before bumping your post.

Now what exactly do you want?  If it's just a list of the Uni's then this will do it...

[code]<?php

// Open the file
$fh = file_get_contents('http://www.collegebookx.com/text.txt');

// Define the pattern
$pattern = '|blank">(.*?)</A>|';

// Match patern and place into an array
preg_match_all($pattern, $fh, $matches);

// Echo the list
foreach ($matches[1] as $u){
  echo "$u<br>\n";
}
?>[/code]

Regards
Huggie
Link to comment
Share on other sites

I've been hacking away and this code will give you an array keyed on state... the value of this array is another array keyed numerically with the values as the colleges...

[code]<?php

// Open the file and get the contents into a single string
$fh = file_get_contents('http://www.collegebookx.com/text.txt');

// Define the pattern for matching the states and colleges
$pattern = '|<H2.*?">(.*?)</H2>(.*?all">)|s';

// Match patern and place into an array
preg_match_all($pattern, $fh, $matches);

// Sort the matches into seperate arrays
$states = $matches[1];
$collegelinks = $matches[2];

// Loop through each state and get the colleges for it
foreach ($states as $n => $s){
  // Define the pattern for matching the colleges without the http link
  $pattern = '|blank">(.*?)</A|s';

  // Match patern and place into an array
  preg_match_all($pattern, $collegelinks[$n], $c);

  // Insert the colleges into arrays keyed on state
  $complete_list[$s] = $c[1];
}

// Sort the array alphabetically on state
ksort($complete_list);

//Print the list...
foreach ($complete_list as $s => $cl){
  echo "<p>\n";
  echo "<b>$s</b><br>\n";
  foreach ($complete_list[$s] as $c){
      echo "$c<br>\n";
  }
  echo "</p>\n";
}
?>[/code]

Regards
Huggie
Link to comment
Share on other sites

Wow, you rock dude.

That code works awesome.

I'm going to study your pregs and such to understand them better and then work on pulling out the tags next to the States and also try and make a small array of what those damned &amp; and such mean so I can successfully change those to actual characters

Your help is very much appreciated :)
Link to comment
Share on other sites

Don't worry about the 'bumping' thing, it's just one of those pet hates of mine... The forum does allow bumping and in the posting guidelines it says 'after a few hours' so no harm done.  As for the code, no problem, I like a good challenge every now and again.

You could probably refine that into a single regex rather than two, but my mind really was a blank yesterday.

Regards
Huggie
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.