PHP 字符串数组仅显示最后一个结果


PHP string array displaying only the last result

需要帮助仅显示以下代码中的最后一个数组:

 $string = "localhost/project/vanilla_2/index.php?p=/discussions/MostCommented";
 /* Use tab and newline as tokenizing characters as well  */
 $tok = strtok($string, "/");
 while ($tok !== false) {
  echo "Word=$tok<br />";
    $tok = strtok("/");

截至目前,我得到以下结果:

  Word=localhost
  Word=project
  Word=vanilla_2
  Word=index.php?p=
  Word=discussions
  Word=MostCommented

问题:如何只得到最后的结果?(评论最多)

谢谢

我相信

您正在尝试从URL获取最后一个字符串。检查以下行

$string = "localhost/project/vanilla_2/index.php?p=/discussions/MostCommented";
$explode_str = explode("/", $string);
echo end($explode_str);