views:

251

answers:

2

I have a text file that I am displaying in a table. I am using preg_match_all to find a specific Title with a specific Chapter and I am replacing the Title and Chapter with preg_replace to make it a link..

For example the contents within the text file are as follows:

Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44

And I am replacing the Title's and Chapters... (i.e. Naruto 123) with a link to the webpage where that is located.

I also need to take into effect the folderpath that the webpage is located in.

  • The folderpath is the title of the anime. So if we were doing it for Naruto 123 the folder path is Naruto/.

So in the end the link will look like this:

http://website/folderpath/animetitle animechapter

The problem that I have is that I can get the folderpath's correct but I cannot create 2 or more distinct links. My code replaces Naruto 123 and Naruto 98 with the same link.

Here is what my code:

  <?

  $data=file_get_contents('series-updates.txt'); //get data from file

$regexp[0]="/(Naruto)[[:space:]](\w+)/";

$regexp[1]="/Naruto/";

preg_match($regexp[0], $data, $matches); //match Manga Title with Chapter for URL

$url= $matches[0];

preg_match($regexp[1], $data, $matches2); //match Manga Title for folderpath

$folderpath= $matches2[0];

$patterns= '/(Naruto)[[:space:]](\w+)/';

$replacements= '<a href="'.$folderpath.'/'.$url.'">'.$url.'</a>';

$data=preg_replace($patterns,$replacements, $data);

  $dat=explode("\n",$data); //split data at new lines

  echo '<table cellspacing=0>';

  foreach ($dat AS $value) { //loop

echo '<tr><td>'.$value.'</td></tr>';

  }

  echo '</table>';

  ?>

here is an the output of the code:

http://xennetworks.com/output3.php

** ALSO, the reason why in the php code I am using preg_match instead of preg_match_all is because if I use preg_match_all for the links I get the output of ARRAY and I wanted you to see the outcome that I would like.

A: 

Try this on for size though I'm not sure what you're looking for the link URL:

$s= <<<STR
Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44
STR;

preg_match_all('/\w{3}\s\d{2}\s\d{2}:\d{2}\s(.+)?\s(\d{2,})/', $s, $m);

for ($i=0; $i<count($m[1]); $i++) {
 $url= sprintf('http://xennetworks.com/%s %s', $m[1][$i], $m[2][$i]);
 echo("$url\n");
}
pygorex1
I'm playing with what you gave me.. but in the meantime...The like link should look like this: http://xennetworks.com/output3.php
D3V1NE
A: 
<?php
$filedata = "Dec 04 20:15 Naruto 123
Dec 04 17:42 Naruto 98
Dec 04 16:19 D Gray Man 001
Dec 04 16:05 Bleach 128
Dec 04 12:13 50 x 50 44";

$lines = explode("\n", $filedata);

echo "<table border=\"1\">";

foreach($lines as $line)
{
echo "<tr>";
preg_match("/^([a-zA-Z]{3}\s\d{2}\s\d{2}:\d{2})\s(.+?)\s(\d+)\s*?$/", $line, $matches);
echo "<td>$matches[1]</td>";
echo "<td><a href=\"/$matches[2]/$matches[2] $matches[3]\">$matches[2] $matches[3]</a></td>";
echo "</tr>";
}
echo "</table>"
?>
D3V1NE