The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Substr not working with variables
Discuss Substr not working with variables in the PHP Development forum on Dev Shed. Substr not working with variables PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 20th, 2013, 03:04 AM
|
|
Contributing User
|
|
Join Date: May 2004
Location: Copenhagen, Denmark
|
|
|
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!
|

January 20th, 2013, 04:59 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
And I take it you had a $pgtitle=wp_title(); before that line? Or
PHP Code:
$pgtitle = substr(wp_title(), 0, 4); echo $pgtitle;
|

January 20th, 2013, 07:47 AM
|
|
Contributing User
|
|
Join Date: May 2004
Location: Copenhagen, Denmark
|
|
Quote: | Originally Posted by requinix And I take it you had a $pgtitle=wp_title(); before that line? Or
PHP Code:
$pgtitle = substr(wp_title(), 0, 4); echo $pgtitle;
| Yes, the whole code is
PHP Code:
$pgtitle = wp_title(); $pgtitle2 = substr($pgtitle, 0, 4); 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($pgtitle, 0, 4); 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 07:57 AM.
|

January 20th, 2013, 10:35 AM
|
 |
Lost in code
|
|
|
|
|
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.
|

January 20th, 2013, 02:25 PM
|
|
Contributing User
|
|
Join Date: May 2004
Location: Copenhagen, Denmark
|
|
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), 0, 4); echo $pgtitle;
Only the first four characters of the page title are echoed 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|