PHP:将匹配的文本替换为querystring [英] PHP: Replace Matched text with querystring

查看:87
本文介绍了PHP:将匹配的文本替换为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数组,并且必须使用链接文本的第一个值作为数组索引来获取相应的值. /p>

检查以下代码段:-

 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

I have a list of abbreviations like verses:

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

And I am currently jquery to convert these links:

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

and makes them as follows:

<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>

The script being used to that is:

$(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
  });
});

What I need is to translate the text between > < to the appropriate number (Mt=80, Lu=81, Rv=92, Nd=95.. etc.), so the converted links becomes like:

<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>

解决方案

You need to create a jQuery array with your pre-defined values and you have to use first value of link text as an array index to get corresponding value.

Check the below snippet:-

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>

Note:-

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

这篇关于PHP:将匹配的文本替换为querystring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆