[英]Using the Preg_Replace_Callback Function
Okay, I think the reason why the search bar on this page is broken is because the PHP updated, and preg_replace
is deprecated. 好的,我认为此页面上的搜索栏损坏的原因是因为PHP已更新,而
preg_replace
已弃用。 https://sparklewash.com/ https://sparklewash.com/
I tried replacing the preg_replace
function to preg_replace_callback
like so, but I'm still getting some issues. 我试着更换
preg_replace
函数preg_replace_callback
像这样,但我仍然得到一些问题。
Original: 原版的:
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
New Version: 新版本:
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace_callback('/(^|_)([a-z])/',
create_function ('$matches', 'return strtoupper($matches[2]);'), $string); // Removes special chars.
}
I apologize if this is easy for you, I was trying to follow an article on here but I'm still relatively new to PHP. 抱歉,这对您来说很简单,我试图在此处关注一篇文章,但对于PHP来说我还是相对较新。
Edit: I belive the preg_replace isn't what broke it due to some of the comments. 编辑:我相信preg_replace不是由于某些评论而使它崩溃的原因。 I've made a new question here to stay on topic: Redirect Loop on $_GET Request
我在这里提出了一个新问题,继续关注: $ _GET请求上的重定向循环
I would not recommend the syntax you are using, the most likely cause of the error, kindly try below syntax. 我不建议您使用的语法(最有可能导致错误的原因),请尝试使用以下语法。
$result = preg_replace_callback('/(^|_)([a-z])/', function($matches){
return strtoupper($matches[0]);
/*
$matches[0] is the complete match of your regular expression
$matches[1] is the match of the 1st round brackets () similarly for $matches[2]...and so on.
*/
}, $string);
//Also $result will contain the resultant string
You have to just pass the $matched to your callback function, you can also declare the callback separately, as an stand alone function. 您只需将$ matched传递给回调函数,也可以将回调单独声明为独立函数。
function make_upper($matches){
return strtoupper($matches[0]);
}
$result = preg_replace_callback('/(^|_)([a-z])/','make_upper' , $string);
Hope my solution works for you, Thanks. 希望我的解决方案对您有用,谢谢。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.