duanpo6079 2010-11-26 14:41
浏览 43
已采纳

drupal ---主题代码

     $i = 0;
   $suggestion = 'page';
   $suggestions = array();
  while ($arg = arg($i++)) {
  $arg = str_replace(array("/", "\\", "\0"), '', $arg);
   $suggestions[] = $suggestion . '-' . $arg;
   if (!is_numeric($arg)) {
    $suggestion .= '-' . $arg;
   }
 }

i am a newbie of drupal,i can't follow the above code well, hope someone can explain it to me.i know the first line is assign the 0 to $i,then assign 'page' to an array. and i know arg is an array in drupal. for example, now the url is example.com/node/1. how to use this url to use the above code.

  • 写回答

2条回答 默认 最新

  • dongzhan1492 2010-11-26 15:35
    关注

    It looks like its purpose is to generate ID strings (probably for a CSS class) depending on paths and excludes numeric components of the path out of the generated ID. For example, 'my/123/article' produces the ID "page-my-article".

    It seems this comes from a function (because the loop reads parameters using arg()) and that it expects Drupal paths such as "node/123/edit".

    So the function would be called something like that:

    mystery_function("my/123/article", "even/better/article");
    

    Variables:

    $i is the variable that stores the loop index

    $suggestion is a String that store the generated ID. It is initialized to "page" because the ID is meant to have the syntax "page-SOMETHING".

    $arg comes from the while loop: it reads the parameters passed to the mystery function one by one

    $suggestions is an array that contains the generated IDs, one per argument passed to the mystery function.


    In the loop:

    The "$arg = str_replace..." line removes unwanted characters like "\" (however that line could definitely be improved).

    The "$suggestions[] = ..." line adds the ID to the array of results.

    The "if (!is_numeric($arg)..." line excludes numbers from the generated ID (e.g. "my/123/article" is probably supposed to produce "my-article")

    The "$suggestion .= ..." line appends the value of "$arg" to the value of "$suggestion" and stores it in "$suggestion"


    But honestly, I wouldn't advise to use that code: I doubt it works as intended given $suggestion isn't reinitialized at each loop so the value of the first path will get attached to the second, and to the third, and so on, and I doubt that is intentional.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化