我有一个字符串,由于'&q'(我猜)在原始字符串中被转义而过早终止。如果我想在PHP中保留原始字符串,我该如何处理?
原始字符串'http://answers.onstartups.com/search?tab=active&q=fbi'
var_dump的结果
'["http://answers.onstartups.com/search?tab=active'
JS
var linksStr = $("#links").val();
var matches = JSON.stringify(linksStr.match(/'bhttps?:'/'/[^'s]+/gi));
$.ajax({
type: 'GET',
dataType: 'json',
cache: false,
data: 'matches=' + matches,
url: 'publishlinks/check_links',
success:
function(response) {
alert(response);
}
})
check_links
$urls = $this->input->get('matches');
var_dump($urls);
可以对JSON字符串进行编码:
data: 'matches=' + encodeURIComponent(matches),
你也可以这样写:
data: { matches: matches }
,然后jQuery应该为你做编码步骤
从jQuery .val()返回的url为:
'http://answers.onstartups.com/search?tab=active&q=fbi'
.match()
正则表达式将返回一个数组:
new Array("http://answers.onstartups.com/search?tab=active&q=fbi")
哪个JSON.stringify()正确输出为:
["http://answers.onstartups.com/search?tab=active&q=fbi"]
但是,如果您将其附加为raw GET参数:
data: 'matches=' + matches,
则URL中的转义&
将终止GET值。使用encodeURIComponent
Change data: 'matches=' + matches,
To: data: {"matches": matches},
.
以便jQuery为您找出编码。否则,必须使用encodeuriccomponent ()