PHP: 用查询字符串替换匹配的文本

PHP: Replace Matched text with querystring

我有一个缩写列表,比如经文:

Mt=80
Lu=81
Rv=92
Nd=95
etc.

我目前正在 jquery 转换这些链接:

<a href="page.php?q=Mt 5: 2">Mt 5: 2</a>
<a href="page.php?q=Mt 5: 2">Nd 14: 25</a>

并制作如下:

<a href="page.php?book=Mt&chapter=5&vmin=2">Mt 5: 2</a>
<a href="page.php?book=Nd&chapter=15&vmin=25">Nd 14: 25</a>

正在使用的脚本是:

$(document).ready(function() {
  $("a[href='page.php']").each(function(index, element){
    href = $(element).attr('href'); // get the href
    text = $(element).text().split(' '); // get the text and split it with space
    $(element).attr('href', href + "?book=" +$.trim(text[0])+"&chapter="+$.trim(text[1].slice(0,-1))+"&vmin="+$.trim(text[2])); //create desired href and replace it with older-one
  });
});

我需要的是将> <之间的文字翻译成合适的数字(Mt=80,Lu=81,Rv=92,Nd=95..等),这样转换后的链接就变成了喜欢:

<a href="page.php?book=80&chapter=5&vmin=2">Mt 5: 2</a>
<a href="page.php?book=95&chapter=15&vmin=25">Nd 14: 25</a>

您需要使用您的预定义值创建一个 jQuery 数组,并且您必须使用 link 文本的第一个值作为数组索引来获取相应的值。

检查以下代码段:-

var myarray = {'Mt':80, 'Lu':81, 'Rv':92, 'Nd':95};// you can add more values
$(document).ready(function() {
  $("a[href='page.php']").each(function(index, element){
    href = $(element).attr('href'); // get the href
    text = $(element).text().split(' '); // get the text and split it with space
    $(element).attr('href', href + "?book=" +$.trim(myarray[text[0]])+"&chapter="+$.trim(text[1].slice(0,-1))+"&vmin="+$.trim(text[2])); //create desired href and replace it with older-one
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="page.php">Mt 5: 2</a><br>
<a href="page.php">Nd 14: 25</a>

注:-

myarray[text[0]] == myarray['Mt'] ==80; //.... so on for other values as well