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

    Join Date
    May 2004
    Location
    Copenhagen, Denmark
    Posts
    171
    Rep Power
    31

    Substr not working with variables


    I've been trying to use the substr() function but I'm having a problem with variables and it's driving me crazy.

    Here is the code. wp_title() is a WordPress tag.

    <?php $pgtitle = wp_title(); echo $pgtitle; ?>
    So far so good, everything works. The code outputs the title of the page.

    <?php $pgtitle2 = substr($pgtitle, 0, 4); echo $pgtitle2; ?>
    This does NOT work. Nothing is outputed. I don't get it. If I use something like 'abcde' instead of $pgtitle the code works. Help!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    And I take it you had a $pgtitle=wp_title(); before that line? Or
    PHP Code:
    $pgtitle substr(wp_title(), 04); echo $pgtitle
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Copenhagen, Denmark
    Posts
    171
    Rep Power
    31
    Originally Posted by requinix
    And I take it you had a $pgtitle=wp_title(); before that line? Or
    PHP Code:
    $pgtitle substr(wp_title(), 04); echo $pgtitle
    Yes, the whole code is

    PHP Code:
     $pgtitle wp_title(); $pgtitle2 substr($pgtitle04); echo $pgtitle2
    That actually does print the page title, but it prints the entire title, and not just what I'm trying to extract with the substr() function. Moreover, if I write this (like my original example):

    PHP Code:
     $pgtitle wp_title(); echo $pgtitle$pgtitle2 substr($pgtitle04); echo $pgtitle2
    I only get one of them printed. I'm not sure if it's the $pgtitle or the $pgtitle2 that is being printed but it's very strange that only one of them gets printed.
    Last edited by FRUGiHOYi; January 20th, 2013 at 08:57 AM.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    A lot of WordPress functions echo their return value by default rather than returning it. (Yet another consequence of WordPress having been built by front-end people without input from back-end architects)

    Take a look at the documentation page for wp_title, because I believe that is the case here. However, also note that the documentation seems to be wrong regarding the second parameter.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Copenhagen, Denmark
    Posts
    171
    Rep Power
    31
    Figured it out. I have to use the WordPress function the_title() instead of wp_title. If I pass the last parameter as FALSE, then the title of the page is returned for use in PHP and I can use the substr() function on it. So the entire code is

    PHP Code:
    $pgtitle substr(the_title('','',FALSE), 04); echo $pgtitle
    Only the first four characters of the page title are echoed

IMN logo majestic logo threadwatch logo seochat tools logo