2017-10-04 163 views
1

更换匹配的文本我有这样的诗句缩写列表:PHP:与查询字符串

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,陆= 81,RV = 92,N D = 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> 
+0

为什么'14:25'被转换成'&章= 15&VMIN = 25' ? (为什么'14:'变成'chapter = 15'? – Justinas

+0

感谢您的回复..只是分别.. trim()用于删除多余的空格,如果该值有任何(前导/尾随空格)和slice()用于删除:从第二个值开始 - 所以第一个是这本书,第二个是章节,第三个是vmin – Mike

+0

@Mike HE正在谈论'Nd 14: 25'这里'chapter = 15'需要'chapter = 14' –

回答

1

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

检查下面的代码片段: -

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

+0

很好,谢谢..但是,当我尝试在数组中添加阿拉伯语它不起作用..像{'كم':80} ..任何建议吗?它需要某种编码或什么? – Mike

+0

你能指出我是什么应该找什么东西?谢谢你的耐心...... – Mike

+0

你在这里做错了,它需要像'{'Mt':'阿拉伯文字'} –