Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我应该使用str_replace而不是substr吗?_Php_String_Replace - Fatal编程技术网

Php 我应该使用str_replace而不是substr吗?

Php 我应该使用str_replace而不是substr吗?,php,string,replace,Php,String,Replace,我正在使用一个数据提要插件,它有一个定制的PHP函数,允许我重写提要中的每个Buy_URL。例如,一个原始的Buy_URL是: http://www.affiliatecompa.com/product/clean.com?ref=ab 我想重写URL的开头和结尾 “” 及 “laik”`分别是。使之成为: http://www.dsqce.com/click-111111-1111XX111?url=http%3A%2F%2Fwww.affiliatecompa.com/product/cl

我正在使用一个数据提要插件,它有一个定制的PHP函数,允许我重写提要中的每个Buy_URL。例如,一个原始的Buy_URL是:

http://www.affiliatecompa.com/product/clean.com?ref=ab
我想重写URL的开头和结尾

“”

“laik”`分别是。使之成为:

http://www.dsqce.com/click-111111-1111XX111?url=http%3A%2F%2Fwww.affiliatecompa.com/product/clean.com?ref=laik
我联系了插件的作者,他告诉我把下面的代码放在主题的function.php中,然后在插件中调用函数

function WOKI_Change_Url($x){
    $y = substr($x, 29);
    $y = substr($y, -2);
    return "http://www.dsqce.com/click-111111-1111XX111?url=http%3A%2F%2Fwww.affiliatecompa.com" . $y . 'laik';
}
显然,它不起作用,因为它删除了URL的任何其他部分,而每个Buyurl现在都变成了

http://www.dsqce.com/click-111111-1111XX111?url=http%3A%2F%2Fwww.affiliatecompa.comlaik
我怀疑substr不适合我在这种情况下想做的事情。我应该在函数中使用str_replace吗

str_replace()
可以满足您的要求,但他的方法也可以。他只是在
substr()
上遗漏了一个参数。他的代码应该与添加回来的参数一起工作(假设字符串的
ref=ab
部分始终有一个2字符的值:

function WOKI_Change_Url($x){
    $y = substr($x, 29);
    $y = substr($y, 0, -2); //the 0 here tells it to use the whole string, minus the last two chars; without the zero, this would muck things up a lot
    return "http://www.dsqce.com/click-111111-1111XX111?url=http%3A%2F%2Fwww.affiliatecompa.com" . $y . 'laik';
}
至于
str\u replace()
,如果您知道要替换的内容的值,这也可能会起作用,但是使用str\u replace进行转义会使事情变得复杂。如果总是用
ref=laik
替换
ref=laik
,那么以下操作应该会起作用(使用
urlencode()
执行转义):

请注意,如果您不确定要替换的始终是
ab
,则可能需要使用正则表达式替换
ref=
之后的任何内容。例如:

function WOKI_Change_Url($x){
    $y = preg_replace("/ref=.*/", "ref=laik", $x); //this replaces the ref= part for anything... but I haven't tested it
    return "http://www.dsqce.com/click-111111-1111XX111?url=http%3A%2F%2Fwww.affiliatecompa.com" . urlencode($y);
}

请查看函数:,我感谢您的帮助。我尝试了所有函数,但不幸的是,它们都不起作用。substr()只是将“”和“laik”放在一起,而不是重写URL的开头和结尾。preg_replace和str_replace()只是将整个URL替换为“”。我会继续努力。谢谢。我开始觉得有什么东西阻止了源url作为参数传递到函数中。你能用调用
WOKI\u Change\u url
函数的相关代码的示例更新这个问题吗?你描述的行为符合如果
$x
没有值。您可以通过添加
echo$x;
作为每个函数的第一行来验证这一点,或者如果这不起作用,可以在一些测试中使用
return$x;
来查看函数对源url的值。谢谢Jeffrey。您的意思是在“function WOKI\u Change\u url($x){之前添加echo$x?否-我的意思是在该行之后立即添加它。但是,您可能看不到echo语句的输出,因此,如果在该行之后立即添加它似乎没有任何作用,请使用
return$x;
function WOKI_Change_Url($x){
    $y = preg_replace("/ref=.*/", "ref=laik", $x); //this replaces the ref= part for anything... but I haven't tested it
    return "http://www.dsqce.com/click-111111-1111XX111?url=http%3A%2F%2Fwww.affiliatecompa.com" . urlencode($y);
}