سؤال

أعتقد أن هذا البرنامج النصي له أهمية كبيرة لأي شيء هنا :) بما في ذلك أنا :)

ما أريد إنشاؤه هو رمز صغير يمكنني استخدامه في أي ملف وسأقوم بإنشاء خبز مثل هذا:

إذا تم استدعاء الملف "website.com/templates/index.php"يجب أن تظهر الخبز:

Website.com > Templates

^^الارتباط^^نص عادي

إذا تم استدعاء الملف "website.com/templates/template_some_name.php"يجب أن تظهر الخبز:

Website.com > Templates > Template Some Name

^^رابط^^رابط^^نص عادي

هل كانت مفيدة؟

المحلول

حسنًا ، من الأمثلة التي أعطيتها يبدو مثل "$ _server ['request_uri'] و ينفجر() يمكن أن تساعدك الوظيفة. يمكنك استخدام Explode لتفكيك عنوان URL بعد اسم المجال إلى صفيف ، وفصله في كل مرفوع إلى الأمام.

كمثال أساسي للغاية ، يمكن تنفيذ شيء مثل هذا:

$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
    echo ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
}

نصائح أخرى

قد يكون هذا مبالغة في خبز بسيط ، لكنه يستحق اللقطة. أتذكر أن هذه المشكلة منذ وقت طويل عندما بدأت لأول مرة ، لكنني لم أحلها أبدًا. هذا هو ، حتى قررت أن أكتب هذا الآن. قون

لقد قمت بتوثيق أفضل ما أستطيع ، في أسفل 3 حالات الاستخدام الممكنة. يتمتع! (لا تتردد في طرح أي أسئلة قد تكون لديكم)

<?php

// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = Array("<a href=\"$base\">$home</a>");

    // Find out the index for the last value in our path array
    $last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path AS $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last)
            $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
        // Otherwise, just display the title (minus)
        else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>

<p><?= breadcrumbs() ?></p>
<p><?= breadcrumbs(' > ') ?></p>
<p><?= breadcrumbs(' ^^ ', 'Index') ?></p>

صنع أيضًا برنامج نصي صغير باستخدام RDFA (يمكنك أيضًا استخدام microdata أو تنسيقات أخرى) تحقق من ذلك على Googleهذا البرنامج النصي يبقى في الاعتبار بنية موقعك.

function breadcrumbs($text = 'You are here: ', $sep = ' &raquo; ', $home = 'Home') {
//Use RDFa breadcrumb, can also be used for microformats etc.
$bc     =   '<div xmlns:v="http://rdf.data-vocabulary.org/#" id="crums">'.$text;
//Get the website:
$site   =   'http://'.$_SERVER['HTTP_HOST'];
//Get all vars en skip the empty ones
$crumbs =   array_filter( explode("/",$_SERVER["REQUEST_URI"]) );
//Create the home breadcrumb
$bc    .=   '<span typeof="v:Breadcrumb"><a href="'.$site.'" rel="v:url" property="v:title">'.$home.'</a>'.$sep.'</span>'; 
//Count all not empty breadcrumbs
$nm     =   count($crumbs);
$i      =   1;
//Loop the crumbs
foreach($crumbs as $crumb){
    //Make the link look nice
    $link    =  ucfirst( str_replace( array(".php","-","_"), array(""," "," ") ,$crumb) );
    //Loose the last seperator
    $sep     =  $i==$nm?'':$sep;
    //Add crumbs to the root
    $site   .=  '/'.$crumb;
    //Make the next crumb
    $bc     .=  '<span typeof="v:Breadcrumb"><a href="'.$site.'" rel="v:url" property="v:title">'.$link.'</a>'.$sep.'</span>';
    $i++;
}
$bc .=  '</div>';
//Return the result
return $bc;}

استعمال parse_url ثم إخراج النتيجة في حلقة:

$urlinfo = parse_url($the_url);
echo makelink($urlinfo['hostname']);
foreach($breadcrumb in $urlinfo['path']) {
  echo makelink($breadcrumb);
}

function makelink($str) {
  return '<a href="'.urlencode($str).'" title="'.htmlspecialchars($str).'">'.htmlspecialchars($str).'</a>';
}

(كود مزيف)

لقد بدأت بالرمز من دومينيك بارنز ، ودمجت ردود الفعل من CWODER وما زلت أواجه مشاكل مع فتات الخبز في المستوى الثالث عندما استخدمت دليلًا فرعيًا. لذلك قمت بإعادة كتابته وأدرجت الرمز أدناه.

لاحظ أنني قمت بإعداد بنية موقع الويب الخاصة بي بحيث يتم إعداد الصفحات التي يجب أن تكون تابعة لـ (مرتبطة من) صفحة على مستوى الجذر على النحو التالي:

  • قم بإنشاء مجلد بنفس الاسم الدقيق للملف (بما في ذلك الرسملة) ، ناقص اللاحقة ، كمجلد على مستوى الجذر

  • ضع جميع الملفات/الصفحات التابعة في هذا المجلد

(على سبيل المثال ، إذا أردت صفحات SoboRINATINE للعملاء. php:

  • قم بإنشاء مجلد يسمى العملاء في نفس مستوى العملاء. php

  • إضافة ملف index.php إلى مجلد العملاء الذي يعيد التوجيه إلى صفحة الاتصال للمجلد (انظر أدناه للحصول على الرمز)

سيعمل هذا الهيكل لمستويات متعددة من المجلدات الفرعية.

فقط تأكد من اتباع بنية الملف الموضحة أعلاه وأدخل ملف index.php مع الكود الموضح في كل مجلد فرعي.

يشبه الرمز في صفحة index.php في كل مقلع فرعي:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Redirected</title>
</head>
<body>
<?php 
$root_dir = "web_root/" ;
$last_dir=array_slice(array_filter(explode('/',$_SERVER['PHP_SELF'])),-2,1,false) ;
$path_to_redirect = "/".$root_dir.$last_dir[0].".php" ; 
header('Location: '.$path_to_redirect) ; 
?>
</body>
</html>

إذا كنت تستخدم الدليل الجذر للخادم كجذر الويب الخاص بك (أي/var/www/html) ، فقم بتعيين $ root_dir = "": (لا تترك trakeing "/" in). إذا كنت تستخدم دليلًا فرعيًا لموقع الويب الخاص بك (أي/var/www/html/web_root ، فقم بتعيين $ root_dir = "web_root/" ؛ (استبدل web_root بالاسم الفعلي لدليل الويب الخاص بك) (تأكد

على أي حال ، إليك رمز (مشتق):

<?php

// Big Thank You to the folks on StackOverflow
// See http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb
// Edited to enable using subdirectories to /var/www/html as root
// eg, using /var/www/html/<this folder> as the root directory for this web site
// To enable this, enter the name of the subdirectory being used as web root
// in the $directory2 variable below
// Make sure to include the trailing "/" at the end of the directory name
// eg use      $directory2="this_folder/" ;
// do NOT use  $directory2="this_folder" ;
// If you actually ARE using /var/www/html as the root directory,
// just set $directory2 = "" (blank)
// with NO trailing "/"

// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ' , $home = 'Home') 
{

    // This sets the subdirectory as web_root (If you want to use a subdirectory)
    // If you do not use a web_root subdirectory, set $directory2=""; (NO trailing /)
    $directory2 = "web_root/" ;

    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ;
    $path_array = array_filter(explode('/',$path)) ;

    // This line of code accommodates using a subfolder (/var/www/html/<this folder>) as root
    // This removes the first item in the array path so it doesn't repeat
    if ($directory2 != "")
    {
    array_shift($path_array) ;
    }

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'. $directory2 ;

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = Array("<a href=\"$base\">$home</a>") ;

    // Get the index for the last value in our path array
    $last = end($path_array) ;

    // Initialize the counter
    $crumb_counter = 2 ;

    // Build the rest of the breadcrumbs
    foreach ($path_array as $crumb) 
    {
        // Our "title" is the text that will be displayed representing the filename without the .suffix
        // If there is no "." in the crumb, it is a directory
        if (strpos($crumb,".") == false)
        {
            $title = $crumb ;
        }
        else
        {
            $title = substr($crumb,0,strpos($crumb,".")) ;
        }

        // If we are not on the last index, then create a hyperlink
        if ($crumb != $last)
        {
            $calling_page_array = array_slice(array_values(array_filter(explode('/',$path))),0,$crumb_counter,false) ;
            $calling_page_path = "/".implode('/',$calling_page_array).".php" ;
            $breadcrumbs[] = "<a href=".$calling_page_path.">".$title."</a>" ;
        }

        // Otherwise, just display the title
        else
        {
            $breadcrumbs[] = $title ;
        }

        $crumb_counter = $crumb_counter + 1 ;

    }
    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs) ;
}

// <p><?= breadcrumbs() ? ></p>
// <p><?= breadcrumbs(' > ') ? ></p>
// <p><?= breadcrumbs(' ^^ ', 'Index') ? ></p>
?>

مرحبًا دومينيك ، كانت إجابتك لطيفة ولكن إذا كان لديك موقع مثل http: //localhost/project/index.php يتكرر رابط "المشروع" لأنه جزء من قاعدة $ ويظهر أيضًا في صفيف المسار $. لذلك قمت بتعديل وأزلت العنصر الأول في صفيف المسار $.

//Trying to remove the first item in the array path so it doesn't repeat
array_shift($path);

لا أعرف ما إذا كانت هذه هي الطريقة الأكثر أناقة ، لكنها تعمل الآن بالنسبة لي.

أضيف هذا الرمز قبل هذا الرمز على السطر 13 أو شيء من هذا القبيل

// Find out the index for the last value in our path array
$last = end(array_keys($path));

فيما يلي عبارة عن خبز ديناميكي بسيط للغاية (تعديل حسب الحاجة):

    <?php 
    $docroot = "/zen/index5.php";
    $path =($_SERVER['REQUEST_URI']);
    $names = explode("/", $path); 
    $trimnames = array_slice($names, 1, -1);
    $length = count($trimnames)-1;
    $fixme = array(".php","-","myname");
    $fixes = array(""," ","My<strong>Name</strong>");
    echo '<div id="breadwrap"><ol id="breadcrumb">';
    $url = "";
    for ($i = 0; $i <= $length;$i++){
    $url .= $trimnames[$i]."/";
        if($i>0 && $i!=$length){
            echo '<li><a href="/'.$url.'">'.ucfirst(str_replace($fixme,$fixes,$trimnames[$i]) . ' ').'</a></li>';
    }
    elseif ($i == $length){
        echo '<li class="current">'.ucfirst(str_replace($fixme,$fixes,$trimnames[$i]) . ' ').'</li>';       
    }
    else{
        echo $trimnames[$i]='<li><a href='.$docroot.' id="bread-home"><span>&nbsp;</span></a></li>';
    }
}
echo '</ol>';
?>

أفضل استخدام explode() الوظيفة كما يلي ...

لا تنس استبدال متغير عنوان URL الخاص بك في الارتباط التشعبي href.

<?php 
    if($url != ''){
        $b = '';
        $links = explode('/',rtrim($url,'/'));
        foreach($links as $l){
            $b .= $l;
            if($url == $b){
                echo $l;
            }else{
                echo "<a href='URL?url=".$b."'>".$l."/</a>";
            }
            $b .= '/';
         }
     }
?>

هذا هو الرمز الذي أستخدمه شخصيًا في مواقعي. يعمل خارج الصندوق.

<?php
function breadcrumbs($home = 'Home') {
  global $page_title; //global varable that takes it's value from the page that breadcrubs will appear on. Can be deleted if you wish, but if you delete it, delete also the title tage inside the <li> tag inside the foreach loop.
    $breadcrumb  = '<div class="breadcrumb-container"><div class="container"><ol class="breadcrumb">';
    $root_domain = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'].'/';
    $breadcrumbs = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    $breadcrumb .= '<li><i class="fa fa-home"></i><a href="' . $root_domain . '" title="Home Page"><span>' . $home . '</span></a></li>';
    foreach ($breadcrumbs as $crumb) {
        $link = ucwords(str_replace(array(".php","-","_"), array(""," "," "), $crumb));
        $root_domain .=  $crumb . '/';
        $breadcrumb .= '<li><a href="'. $root_domain .'" title="'.$page_title.'"><span>' . $link . '</span></a></li>';
    }
    $breadcrumb .= '</ol>';
    $breadcrumb .= '</div>';
    $breadcrumb .= '</div>';
    return $breadcrumb;
}
echo breadcrumbs();
?>

CSS:

.breadcrumb-container {
    width: 100%;
    background-color: #f8f8f8;
    border-bottom-color: 1px solid #f4f4f4;
    list-style: none;
    margin-top: 72px;
    min-height: 25px;
    box-shadow: 0 3px 0 rgba(60, 57, 57, .2)
}

.breadcrumb-container li {
    display: inline
}
.breadcrumb {
    font-size: 12px;
    padding-top: 3px
}
.breadcrumb>li:last-child:after {
    content: none
}

.breadcrumb>li:last-child {
    font-weight: 700;
    font-style: italic
}
.breadcrumb>li>i {
    margin-right: 3px
}

.breadcrumb>li:after {
    font-family: FontAwesome;
    content: "\f101";
    font-size: 11px;
    margin-left: 3px;
    margin-right: 3px
}
.breadcrumb>li+li:before {
    font-size: 11px;
    padding-left: 3px
}

لا تتردد في اللعب مع حشوة CSS والهوامش حتى تحصل عليها بشكل صحيح لموقعك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top