英文:
Passing var with square bracket into function, like this [1], brackets not recognized PHP 8.2
问题
PHP 8.2:过去三小时我一直在寻找一个从文本中识别方括号的解决方案。
如果我直接将字符串文本放入函数中,我可以识别方括号:
$Start = "[";
$sHaystack = "This ist a test[2] looking for square brackets";
echo "Haystack:". $sHaystack . "<br>";
$position = mb_strpos($sHaystack,$Start);
echo "Position of ".$Start." in haystack is: ".$position."<br>";
完美地工作。
使用preg_replace()也很好用。
但是一旦将字符串传递到函数中,当在回显字符串时,它在视觉上看起来很好,但是方括号既不能用substr()、mb_substr()、preg_replace()识别,也不能用其他任何方法。
同时,尝试使用这些括号“{”或“(”也可以完美地工作,即使在函数调用时输入。
我的推理是在某种程度上需要在将字符串发送到函数中时转义方括号。
我已经尝试过添加反斜杠等,但不起作用。
有人有什么想法吗?
这是函数($sStart = "["和$sEnd="]"):
function identifyBrackets($sHaystack,$sStart,$sEnd){
// 如果在这里定义内容,它可以工作。如果通过函数调用输入,方括号将无法识别。
$sHaystack = "This ist a test[2] looking for square brackets";
// 使用preg_replace进行测试,如果传递其他括号,它可以工作
$what = ['~[[]~', '~[]]~'];
$with = ['{', '}'];
$sHaystack = preg_replace($what, $with, $sHaystack);
echo "Haystack:<br> ".$sHaystack."<br>";
$position = strpos($sHaystack,$sStart);
echo "Position of ".$sStart." in haystack is number: ".$position."<br>"; // 通过函数调用时找不到位置。
return $sHaystack;
英文:
PHP 8.2: The last 3 hours I am searching for a solution to identify square brackets from a text.
In case I place the string text directly in the function, I can identify the square brackets:
$Start = "[";
$sHaystack = "This ist a test[2] looking for square brackets";
echo "Haystack:". $sHaystack . "<br>";
$position = mb_strpos($sHaystack,$Start);
echo "Position of ".$Start." in haystack is: ".$position."<br>";
Works perfectly.
It also works well using preg_replace().
But once the string is delivered into the function while doing the function call, it optically appears well when echoing the string, but the square brackets are not recognized neither with substr(), mb_substr(), preg_replace() and nothing else.
Meanwhile trying the same with this brackets "{" or "(" it works perfectly, also when entered while function call.
My reasoning went to the idea that in some manner the square brackets need to be escaped while the string is being sent into the function.
I already tried adding backslashes etc, but it does not work.
Has anyone got an idea?
This is the function ($sStart = "[" and $sEnd="]"):
function identifyBrackets($sHaystack,$sStart,$sEnd){
// in case content is defined here it works. If entered via function call the square brackets are not identified.
$sHaystack = "This ist a test[2] looking for square brackets";
//test with preg_replace, if other brackets are delivered it works
$what = ['~[[]~', '~[]]~'];
$with = ['{', '}'];
$sHaystack = preg_replace($what, $with, $sHaystack);
echo "Haystack:<br> ".$sHaystack."<br>";
$position = strpos($sHaystack,$sStart);
echo "Position of ".$sStart." in haystack is number: ".$position."<br>"; //with call via function no position found.
return $sHaystack;
And yes, obviously I already searched all resources here on Stack Overflow, but they treat different problems:
https://stackoverflow.com/questions/24510759/capturing-text-between-square-brackets-after-a-substring-in-php
https://stackoverflow.com/questions/9123447/php-preg-replace-square-brackets
https://stackoverflow.com/questions/56636624/how-to-replace-any-brackets-for-rounded-brackets-using-regular-expression
答案1
得分: 1
I think i got the problem, when I runned file_get_contents here, some of the square brackets were html encoded:
So i guess you can use this regex to cover both ways, or search for the encoded characters: &#91;
and &#93;
Regex: &#91;|\[
for [
Regex: &#93;|\]
for ]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论