简体   繁体   English

PHP preg_match_all没有字符或没有特定字符

[英]PHP preg_match_all no character or not a certain character

Right now the test seems to be working for avoiding the characters that I don't want but it's only returning a count of 2. I know why, I just don't know how to address it. 现在,该测试似乎可以避免出现我不希望使用的字符,但是它只返回2。我知道为什么,我只是不知道该如何解决。 The problem is the last ? 问题是最后? is being excluded because the actual match for the 2nd match is (?+ so it's not matching the 3rd since there is no "starting" character for that pattern, it would just be ?) . 被排除在外是因为第二个匹配项的实际匹配是(?+所以它不匹配第三个匹配项,因为该模式没有“开始”字符,而是?)

$pattern =  "/([^\w\d'\"`]\?[^\w\d'\"`])/";
$subject = "`test` = ? and `other` = (?+?)";
$count = preg_match_all($pattern, $subject, $matches);
echo "Count: $count\n"; // echoes 2 instead of 3

Basically, I want to count up all the parameters used, so match all ? 基本上,我想累加所有使用的参数,所以匹配所有? in the $subject with a ? $subject带有? not surrounded by letters, numbers, quotes, and ticks. 不要被字母,数字,引号和刻度所包围。

This is the actual pattern that matters: 这是重要的实际模式:

[^\w\d'\"'`]

Update: For others, miken32's solution is to convert the above pattern to: 更新:对于其他人,miken32的解决方案是将上述模式转换为:

(?=[^\w\d'\"'`])

Try using a lookahead assertion : 尝试使用先行断言

$pattern =  "/((?<=[^\w\d'\"`])\?(?=[^\w\d'\"`]))/";

It will look ahead without moving the search forward. 它将向前看而不会向前搜索。

Edited to add the lookbehind assertion as well. 编辑后还添加了后向断言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM