如何使用具有 2 个值的 preg_match_all 和 preg_replace 来创建 2 个不同的链接

发布于 2024-08-14 04:28:58 字数 1588 浏览 3 评论 0原文

我有一个在表格中显示的文本文件。我正在使用 preg_match_all 查找具有特定章节的特定标题,并用 preg_replace 替换标题和章节以使其成为链接。

例如,文本文件中的内容如下:

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

我正在替换标题和章节...(即火影忍者 123),并带有指向其所在网页的链接。

我还需要使网页所在的文件夹路径生效。

  • 文件夹路径是动漫的标题。因此,如果我们为《火影忍者 123》执行此操作,则文件夹路径为 Naruto/。

所以最后链接将如下所示:

http://website/folderpath/animetitle animechapter

我遇到的问题是我可以获得正确的文件夹路径,但无法创建 2 个或更多不同的链接。我的代码用相同的链接替换了火影忍者 123 和火影忍者 98。

这是我的代码:

  <?

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

  ?>

这是代码的输出:

http://xennetworks.com/output3.php

** 另外,在 php 代码中我使用 preg_match 而不是 preg_match_all 的原因是因为如果我使用 preg_match_all 作为链接,我会得到 ARRAY 的输出,并且我希望你看到我想要的结果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉薄对峙 2024-08-21 04:28:58

尝试一下尺寸,但我不确定您要查找的链接 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");
}

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");
}
往事随风而去 2024-08-21 04:28:58
<?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>"
?>
<?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>"
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文