PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 20th, 2013, 03:04 AM
FRUGiHOYi FRUGiHOYi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Copenhagen, Denmark
Posts: 166 FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 9 m 28 sec
Reputation Power: 30
Send a message via AIM to FRUGiHOYi
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!

Reply With Quote
  #2  
Old January 20th, 2013, 04:59 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,681 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 2 h 20 m 36 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
And I take it you had a $pgtitle=wp_title(); before that line? Or
PHP Code:
 $pgtitle substr(wp_title(), 04); echo $pgtitle

Reply With Quote
  #3  
Old January 20th, 2013, 07:47 AM
FRUGiHOYi FRUGiHOYi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Copenhagen, Denmark
Posts: 166 FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 9 m 28 sec
Reputation Power: 30
Send a message via AIM to FRUGiHOYi
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(), 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 07:57 AM.

Reply With Quote
  #4  
Old January 20th, 2013, 10:35 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
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
How to program a basic, secure login system using PHP

Quote:
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

Reply With Quote
  #5  
Old January 20th, 2013, 02:25 PM
FRUGiHOYi FRUGiHOYi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Copenhagen, Denmark
Posts: 166 FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level)FRUGiHOYi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 9 m 28 sec
Reputation Power: 30
Send a message via AIM to FRUGiHOYi
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Substr not working with variables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap