English 中文(简体)
How do I use preg_match_all with 2 values and preg_replace to create 2 different links
原标题:

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("
",$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.

问题回答

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}sd{2}sd{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
");
}
<?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("
", $filedata);

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

foreach($lines as $line)
{
echo "<tr>";
preg_match("/^([a-zA-Z]{3}sd{2}sd{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>"
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签