php 尝试将正则表达式匹配传递给函数时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9571559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error trying to pass regex match to function
提问by Giri
I'm getting Syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'
我越来越 Syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'
This is the code i'm using
这是我正在使用的代码
function wpse44503_filter_content( $content ) {
$regex = '#src=("|\')'.
'(/images/(19|20)(0-9){2}/(0|1)(0-9)/[^.]+\.(jpg|png|gif|bmp|jpeg))'.
'("|\')#';
$replace = 'src="'.get_site_url( ).'"';
$output = preg_replace( $regex, $replace, $content );
return $output;
}
This is the line where i'm getting that error $replace = 'src="'.get_site_url( $2 ).'"';
这是我收到错误的那一行 $replace = 'src="'.get_site_url( $2 ).'"';
Can anyone help me to fix it? Thanks
任何人都可以帮我修复它吗?谢谢
采纳答案by Joachim Isaksson
What you're trying to do (ie replacing the matched string with the result of a function call) can't be done using preg_replace
, you'll need to use preg_replace_callback
instead to get a function called for every match.
您尝试执行的操作(即用函数调用的结果替换匹配的字符串)无法使用 完成preg_replace
,您需要使用preg_replace_callback
来代替为每个匹配调用一个函数。
A short example of preg_replace_callback;
preg_replace_callback 的一个简短示例;
$get_site_url = // Returns replacement
function($row) {
return '!'.$row[1].'!'; // row[1] is first "backref"
};
$str = 'olle';
$regex = '/(ll)/'; // String to match
$output = preg_replace_callback( // Match, calling get_site_url for replacement
$regex,
$get_site_url,
$str);
var_dump($output); // output "o!ll!e"
回答by Farray
You can't have '$2' as a variable name. It must start with a letter or underscore.
您不能将 '$2' 作为变量名。它必须以字母或下划线开头。
http://php.net/manual/en/language.variables.basics.php
http://php.net/manual/en/language.variables.basics.php
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
变量名遵循与 PHP 中其他标签相同的规则。有效的变量名以字母或下划线开头,后跟任意数量的字母、数字或下划线。作为正则表达式,它将被表示为:'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
EditAbove was my original answer and is the correct answer to the simple "syntax error" question. More in-depth answer below...
上面的编辑是我的原始答案,是对简单的“语法错误”问题的正确答案。下面有更深入的回答...
You are trying to use $2 to represent "the second capture group", but you haven't done anything at that point to match your regex. Even if $2 was a valid PHP variable name, it still wouldn't be set at that point in your script. Because of this, you can determine that you are using preg_replace
improperly and that it may not suit your actual needs.
您正在尝试使用 $2 来表示“第二个捕获组”,但此时您还没有做任何事情来匹配您的正则表达式。即使 $2 是一个有效的 PHP 变量名,它仍然不会在脚本中的那个点被设置。因此,您可以确定您使用preg_replace
不当,并且它可能不适合您的实际需要。
Note that the preg_replace
documentation doesn't support using $n as a separate variable outside of the replacement operation. In other words, 'foo' . $1 . 'bar'
is not a valid replacement string, but 'foo$1bar'
is.
请注意,preg_replace
文档不支持将 $n 用作替换操作之外的单独变量。换句话说,'foo' . $1 . 'bar'
不是有效的替换字符串,而是'foo$1bar'
。
Depending on the complexity of get_site_url
, you have 2 options:
根据 的复杂程度get_site_url
,您有两种选择:
If
get_site_url
is simply adding a root directory or server name, you could change your replacement string tosrc="/myotherlocation$2"
. This will effectively replace "/image/..." with "/myotherlocation/image/..." in the img src. This will not work ifget_site_url
is doing something more complex.If
get_site_url
is complex, you should usepreg_replace_callback
per other answers. Give the documentation a read and post a new question (or I guess update this question?) if you have trouble with the implementation.
如果
get_site_url
只是添加根目录或服务器名称,您可以将替换字符串更改为src="/myotherlocation$2"
. 这将有效地将 img src 中的“/image/...”替换为“/myotherlocation/image/...”。如果get_site_url
正在做一些更复杂的事情,这将不起作用。如果
get_site_url
很复杂,你应该使用preg_replace_callback
per other answers。如果您在实施时遇到问题,请阅读文档并发布一个新问题(或者我想更新这个问题?)。
回答by prodigitalson
PHP variable names cant begin with a number.
PHP 变量名不能以数字开头。
回答by Ignacio Vazquez-Abrams
$2
is not a valid PHP variable. If you meant the second group in the regex then you want to put \2
in a string. However, since you're passing it to a function then you'll need to use preg_replace_callback()
instead and substitute appropriately in the callback.
$2
不是有效的 PHP 变量。如果您指的是正则表达式中的第二组,那么您想放入\2
一个字符串。但是,由于您将它传递给函数,因此您需要preg_replace_callback()
在回调中使用并适当替换。
回答by Krishnamoorthy Acharya
if PHP variable begins with number use following:
如果 PHP 变量以数字开头,请使用以下内容:
when I was getting the following as the result set from thrid party API
当我从第三方 API 获得以下结果集时
Code Works
代码工作
$stockInfo->original->data[0]->close_yesterday
Code Failed
代码失败
$stockInfo->original->data[0]->52_week_low
Solution
解决方案
$stockInfo->original->data[0]->{'52_week_high'}