#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Location
    Chicago, IL
    Posts
    3
    Rep Power
    0

    PHP, Trim function


    I figured this would be more classified as beginner then meriting the PHP development forum, I know I am new to DevShed so any help is greatly appreciated.

    I am working on Interspire CMS/Cart for a client site and trying to give myself some more control for a front end basis. The navigation is dynamically built by this function

    PHP Code:
    function buildMainNavigation()
    {
        
    $output="";
        
    $query="SELECT title FROM iwp_content WHERE typeid=2 OR typeid=5  ORDER BY sortorder";
        
    $result=mysql_query($query);
        
    $output=$output.'<div id="MainNavigation">';
        
    $output=$output.'<div id="MainLinks">';
        
    $output=$output.'<ul>';
        
    $output=$output.'<li><a class="home" href="./">HOME</a></li>';
        while(
    $row mysql_fetch_array($result))
        {
            
    $pagetitle=$row['title'];
            
    $link strtolower($pagetitle);
            
    $link=str_replace(" ","-",$link);
            
    $link=$link.".html";
            
    $pagetitlelink=trim($pagetitle);
            
    $output=$output."<li><a class='$pagetitlelink' href='$link'>$pagetitle</a></li>";
        }
    //end while
        
    $output=$output.'</ul>';
        
    $output=$output.'</div>';
        
    $output=$output.'</div>';
        return 
    $output;
    }
    //end function 
    Originally in the output of each link (excluding home which is always there) it just outputs

    Code:
    <a href="$link">$pagetitlelink</a>
    My goal was to add in the

    PHP Code:
    class='$pagetitle' 
    so that I can control each navigation a href with via it's class; while it does work, the problem is the extra spacing. To fix I attempted to use the trim function on $pagetitle and then create a new variable out of it for the class ($pagetitlelink)

    It still works giving me the page but the trim (as well as ltrim and rtrim) did not remove the extra space. So some of the links with multiple words still read as 'Here Is Link' instead of 'HereIsLink'. My final thought was

    PHP Code:
    $pagetitlelink=trim($pagetitle);
    $output=$output."<li><a class='$pagetitlelink' href='$link'>$pagetitle</a></li>"
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    Beginner Programming is really more for the generic "I'm new to programming"-kind of help. Your question is entirely about PHP so the PHP Forum was the better choice. But don't worry about it (and don't make a thread there - this one is fine, and a moderator will probably move it soon enough anyways).


    trim() only removes spaces from the beginning and end of the string - not from anywhere in the middle. The best way to get what you want would be to use regular expressions to remove any unwanted characters.

    However I question your idea of using the post title to create CSS classes. What is this "more control" that you're trying to get? I bet there's a cleaner and more sensible approach.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    2
    Rep Power
    0
    Well, a simple

    PHP Code:
    str_replace(" ","",$string); 
    should be it

IMN logo majestic logo threadwatch logo seochat tools logo