douyun3022 2019-06-12 00:35
浏览 83

当在带有变量的函数中构建时,PHP会截断锚标记中的参数

PHP truncates parameters from inside an anchor tag whenever I build the code in a PHP function, and use a PHP variable for the href value.

NOTE: While writing this up, I had a brain flash and SOLVED my own problem. I will put the problem and solution here in case others find it useful.

BACKGROUND: I'm building a quasi-mega-menu using jQuery and the modal.js plugin of Bootstrap. Everything worked fine, with the menu button triggering the modal, which displays the HTML code I created for the megamenu, along with a dozen or more page links on that menu. The page links worked fine.

Then I decided to add tooltips to the page links to give visitors a brief description of each page. I did this by adding the CLASS and TITLE attributes to the anchor tag for the link. And this part also worked just fine.

Then, to clean up the links, I put the text of the TITLE attribute into a PHP variable. That also worked just fine.

One of the operational anchor links at the time looked like this:

<a href="cleanscans.php" class="tooltip-test" title="<?php echo($TTClSc); ?>">Cleaning Scans</a>

Finally, I wanted to make it even easier to add links to the list, so I built a PHP function to build the line above, passing in two parameters - the stem filename of the page being called and the text for the link:

function BuildMenuItem($Pageroot, $MenuText)

The function worked perfectly, EXCEPT it always truncated (cut off) all parameters after the closing quote of the href="" statement. In other words, the CLASS and TITLE parameters never made it out of the function.

This was driving me bonkers, and I couldn't figure out why this was happening. I went over it a dozen times, and couldn't figure it out, and that's when I decided I had to come here for help.

As I began writing this posting, it occurred to me that anchor parameters don't need to be furnished in any particular order, so if the function truncates everything after the href, why not put class and title before it?

So I tried it, and VOILA! It Worked! My function now very nicely provides new menu links on demand, complete with tooltips. Like this:

<?php echo BuildMenuItem("procedures", "Procedures") ?>
<?php echo BuildMenuItem("pdftools", "PDF Tools") ?>

The only caveat is I have to name my tooltip text variables with $TT concatenated to the stem file name.

Here's an example of the output of that function, from the Procedures call (first line above):

<a class='tooltip-test' title="$TTprocedures" href="http://www.mywebsitename.com/procedures.php">Procedures</a>

But I'm still flummoxed as to WHY this problem occurred. The function to build the above statement will truncate all the parameters after HREF, but they work fine if listed before.

Any clues why?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 Python安装cvxpy库出问题
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
    • ¥15 python天天向上类似问题,但没有清零
    • ¥30 3天&7天&&15天&销量如何统计同一行
    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 C#调用python代码(python带有库)
    • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题