Page 1 of 1

use of hyphen instead of underscore

Posted: Thu Mar 26, 2009 9:08 am
by wayneob
Hi all :)

Hope someone can help with this? ..I have this php code:

Code: Select all

$convert_toSpace = true;    // true if script should convert _ in folder names to spaces
but when I try to replace with hypen:

Code: Select all

$convert-toSpace = true;    // true if script should convert _ in folder names to spaces
it doesnt work.

Reason for wanting to change to hyphen (-) is that I have used underscore in folder names and have found out that I should of used hyphen instead for better search results! as a hyphen is displayed as blank "my-name" will display "my name" whereas undescore "my_name" is displayed as "my_name".

script used is:

Code: Select all

<?php
 
$convert_toSpace = true; // true if script should convert _ in folder names to spaces
$upperCaseWords = false;    // true if script should convert lowercase to initial caps
$topLevelName = "HOME";     // name of home/root directory
$separator = " > ";      // characters(s) to separate links in hierarchy (default is a > with 2 spaces on either side)
 
// find index page in directory...
function MPBCDirIndex($dir) {
    $index = '';
    @$dir_handle = opendir($dir);
    if ($dir_handle) {
        while ($file = readdir($dir_handle)) {
            $test = substr(strtolower($file), 0, 6);
            if ($test == 'index.') {
                $index = $file;
                break;
                }
            }
        }
    return $index;
    }
 
// make clean array (trim entries and remove blanks)...
function MPBCTrimArray($array) {
    $clean = array();
    for ($n=0; $n<count($array); $n++) {
        $entry = trim($array[$n]);
        if ($entry != '') $clean[] = $entry;
        }
    return $clean;
    }
 
// function to prep string folder names if needed...
function MPBCFixNames($string) {
    global $convert_toSpace;
    global $upperCaseWords;
 if ($convert_toSpace) $string = str_replace('_', ' ', $string);
    if ($upperCaseWords) $string = ucwords($string);
    return $string;
    }
 
$server = (isset($_SERVER)) ? $_SERVER : $HTTP_SERVER_VARS;
 
$htmlRoot = (isset($server['DOCUMENT_ROOT'])) ? $server['DOCUMENT_ROOT'] : '';
if ($htmlRoot == '') $htmlRoot = (isset($server['SITE_HTMLROOT'])) ? $server['SITE_HTMLROOT'] : '';
 
$pagePath = (isset($server['SCRIPT_FILENAME'])) ? $server['SCRIPT_FILENAME'] : '';
if ($pagePath == '') $pagePath = (isset($server['SCRIPT_FILENAME'])) ? $server['SCRIPT_FILENAME'] : '';
 
$httpPath = ($htmlRoot != '/') ? str_replace($htmlRoot, '', $pagePath) : $pathPath;
 
$dirArray = explode('/', $httpPath);
if (!is_dir($htmlRoot.$httpPath)) $dirArray = array_slice($dirArray, 0, count($dirArray) - 1);
 
$linkArray = array();
$thisDir = '';
$baseDir = ($htmlRoot == '') ? '' : $htmlRoot;
for ($n=0; $n<count($dirArray); $n++) {
    $thisDir .= $dirArray[$n].'/';
    $thisIndex = MPBCDirIndex($htmlRoot.$thisDir);
    $thisText = ($n == 0) ? $topLevelName : MPBCFixNames($dirArray[$n]);
    $thisLink = ($thisIndex != '') ? '<a href="'.$thisDir.$thisIndex.'">'.$thisText.'</a>' : $thisText;
    if ($thisLink != '') $linkArray[] = $thisLink;
    }
 
$results = (count($linkArray) > 0) ? implode($separator, $linkArray) : '';
if ($results != '') print('<div class="backlinks">'.$results.'</div>');
 
?>
I tried changing to:

Code: Select all

<?php
 
[color=#FF0000]$convert-toSpace = true; // true if script should convert - in folder names to spaces[/color]
$upperCaseWords = false;    // true if script should convert lowercase to initial caps
$topLevelName = "HOME";     // name of home/root directory
$separator = " > ";      // characters(s) to separate links in hierarchy (default is a > with 2 spaces on either side)
 
// find index page in directory...
function MPBCDirIndex($dir) {
    $index = '';
    @$dir_handle = opendir($dir);
    if ($dir_handle) {
        while ($file = readdir($dir_handle)) {
            $test = substr(strtolower($file), 0, 6);
            if ($test == 'index.') {
                $index = $file;
                break;
                }
            }
        }
    return $index;
    }
 
// make clean array (trim entries and remove blanks)...
function MPBCTrimArray($array) {
    $clean = array();
    for ($n=0; $n<count($array); $n++) {
        $entry = trim($array[$n]);
        if ($entry != '') $clean[] = $entry;
        }
    return $clean;
    }
 
// function to prep string folder names if needed...
function MPBCFixNames($string) {
[color=#FF0000] global $convert-toSpace;[/color]
    global $upperCaseWords;
[color=#FF0000] if ($convert-toSpace) $string = str_replace('-', ' ', $string);[/color]
    if ($upperCaseWords) $string = ucwords($string);
    return $string;
    }
 
$server = (isset($_SERVER)) ? $_SERVER : $HTTP_SERVER_VARS;
 
$htmlRoot = (isset($server['DOCUMENT_ROOT'])) ? $server['DOCUMENT_ROOT'] : '';
if ($htmlRoot == '') $htmlRoot = (isset($server['SITE_HTMLROOT'])) ? $server['SITE_HTMLROOT'] : '';
 
$pagePath = (isset($server['SCRIPT_FILENAME'])) ? $server['SCRIPT_FILENAME'] : '';
if ($pagePath == '') $pagePath = (isset($server['SCRIPT_FILENAME'])) ? $server['SCRIPT_FILENAME'] : '';
 
$httpPath = ($htmlRoot != '/') ? str_replace($htmlRoot, '', $pagePath) : $pathPath;
 
$dirArray = explode('/', $httpPath);
if (!is_dir($htmlRoot.$httpPath)) $dirArray = array_slice($dirArray, 0, count($dirArray) - 1);
 
$linkArray = array();
$thisDir = '';
$baseDir = ($htmlRoot == '') ? '' : $htmlRoot;
for ($n=0; $n<count($dirArray); $n++) {
    $thisDir .= $dirArray[$n].'/';
    $thisIndex = MPBCDirIndex($htmlRoot.$thisDir);
    $thisText = ($n == 0) ? $topLevelName : MPBCFixNames($dirArray[$n]);
    $thisLink = ($thisIndex != '') ? '<a href="'.$thisDir.$thisIndex.'">'.$thisText.'</a>' : $thisText;
    if ($thisLink != '') $linkArray[] = $thisLink;
    }
 
$results = (count($linkArray) > 0) ? implode($separator, $linkArray) : '';
if ($results != '') print('<div class="backlinks">'.$results.'</div>');
 
?>

Re: use of hyphen instead of underscore

Posted: Thu Mar 26, 2009 9:10 am
by php_east
it is of the same reason that you cannot use +.

Re: use of hyphen instead of underscore

Posted: Thu Mar 26, 2009 9:22 am
by wayneob
ummm

so should I remove to backlinks/breadcrumbs from my pages? or do you think it will be ok leaving as it is with underscores??

thx

Re: use of hyphen instead of underscore

Posted: Thu Mar 26, 2009 9:29 am
by php_east
that's up to you, all i'm saying is that logical operators cannot be used in variables.
as for URLs, you would be safe using urlencode. there is a set of forbidden characters for URLs according to RFC. you might want to look it up and stay within accepatable characters.

Re: use of hyphen instead of underscore

Posted: Thu Mar 26, 2009 9:36 am
by wayneob
many thanks!

do you have a link handy?

Re: use of hyphen instead of underscore

Posted: Thu Mar 26, 2009 10:11 am
by php_east