如何使用具有 2 个值的 preg_match_all 和 preg_replace 来创建 2 个不同的链接
我有一个在表格中显示的文本文件。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下尺寸,但我不确定您要查找的链接 URL:
Try this on for size though I'm not sure what you're looking for the link URL: