HTML 实体和角度大括号问题


html entities and angle brace issue

所以,我有这个rel/URL,我正在尝试填充到一个变量中,以便我可以在其他地方打印出来:

$relnext = "<link rel='next' 
                  href='javascript:".$content_pager->PagerName
                                    . "_form." 
                                    . $content_pager->PagerName 
                                    . "PagerPage.value='"" 
                                    . $content_pager->Page+1 
                                    . "'"; " 
                                    . $content_pager->PagerName 
                                    . "DoSubmit();' />"; 

事实上,当我打印出来时,我得到的只是:

1";MediaBoxContentDoSubmit((;'/>

经过一些研究,我"似乎"应该使用htmlentities,但是:

echo htmlentities($relnext);

也只是产生:

1"; MediaBoxContentDoSubmit();' />

这里应该使用其他功能吗?

非常感谢您能提供的任何帮助!

您有一个运算符优先级/关联性问题。 +运算符左侧的 . 运算符在+运算符之前执行,因为它们都是左关联且具有相同的优先级。 您希望首先执行+运算符(以 $content_pager->Page+1 为单位(,然后执行所有.运算符。

实际上,您将使用+运算符将字符串添加到数字 (1(,在这种情况下,字符串(+之前的所有内容(将被视为 0。 这就是为什么第一个字符是1,因为它是"some string"+1的结果,被解释为0+1

因此,您的第一个代码段应该是:

$relnext = "<link rel='next' href='javascript: " . $content_pager->PagerName . "_form." . $content_pager->PagerName . "PagerPage.value='"" . ($content_pager->Page+1) . "'"; " . $content_pager->PagerName . "DoSubmit();' />"; 

请注意,$content_pager->Page+1部分现在位于括号中。

更多信息:

  • http://php.net/manual/en/language.operators.precedence.php