将带有方括号的变量传递给函数,就像这样 [1],PHP 8.2 不识别方括号。

huangapple go评论61阅读模式
英文:

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 = &quot;[&quot;;
	$sHaystack = &quot;This ist a test[2] looking for square brackets&quot;;
	echo &quot;Haystack:&quot;. $sHaystack . &quot;&lt;br&gt;&quot;;
	$position = mb_strpos($sHaystack,$Start);
	echo &quot;Position of  &quot;.$Start.&quot; in haystack is: &quot;.$position.&quot;&lt;br&gt;&quot;;

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 = &quot;This ist a test[2] looking for square brackets&quot;;

        //test with preg_replace, if other brackets are delivered it works
	$what = [&#39;~[[]~&#39;, &#39;~[]]~&#39;];
	$with = [&#39;{&#39;, &#39;}&#39;];
	$sHaystack = preg_replace($what, $with, $sHaystack);

	echo &quot;Haystack:&lt;br&gt; &quot;.$sHaystack.&quot;&lt;br&gt;&quot;;

	$position = strpos($sHaystack,$sStart);
	echo &quot;Position of  &quot;.$sStart.&quot; in haystack is number: &quot;.$position.&quot;&lt;br&gt;&quot;; //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:

将带有方括号的变量传递给函数,就像这样 [1],PHP 8.2 不识别方括号。

So i guess you can use this regex to cover both ways, or search for the encoded characters: &amp;#91; and &amp;#93;

Regex: &amp;#91;|\[ for [

Regex: &amp;#93;|\] for ]

英文:

I think i got the problem, when I runned file_get_contents here, some of the square brackets were html encoded:

将带有方括号的变量传递给函数,就像这样 [1],PHP 8.2 不识别方括号。

So i guess you can use this regex to cover both ways, or search for the encoded characters: &amp;#91; and &amp;#93;

Regex: &amp;#91;|\[ for [

Regex: &amp;#93;|\] for ]

huangapple
  • 本文由 发表于 2023年6月12日 04:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定