Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 02-10-2008, 08:32 PM
MrApples MrApples is offline
 
Join Date: Aug 2007
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Troubles with substr (PHP)

I've been having a load of trouble with the substr function, and I'm starting to think I am mis-using it.

PHP Code:
            $code '
  Events <br />
    Time - Elapsed game time is 300.00 seconds <br />
  Conditions <br />
  Actions <br />'
;
        
$nextline strpos($code,'<br />');
         while ( 
$nextline ){
            
$line substr($code,0,$nextline);
                    echo 
'1LINE ' $line '<br />'// TESTING
            
$code substr($code,$nextline);
                    echo 
'1CODE ' $code '<br />'// TESTING
            
$nextline strpos($code,'<br />');
                    echo 
'NEXTLINE ' $nextline '<br />'// TESTING
        

It works the first time around, but only the first time. On the second the nextline is set to 0 and the loop ends. My only guess is that substr removes "<br />"'s from a string, if thats the case, how can I prevent that?
Reply With Quote
  #2  
Old 02-10-2008, 09:42 PM
Tefra's Avatar
Tefra Tefra is offline
 
Join Date: Mar 2005
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

you basically want to echo each line right ?

PHP Code:
$lines explode("<br />"$code);
foreach(
$lines as $line)
{
    echo 
$line;
    echo 
"<br />";


your way is too complicated for what you want to do, but anyway here it is

PHP Code:
$codelen  strlen($code);
$nextline strpos($code,'<br />');
 while ( 
$nextline ){
    
$line substr($code,0,$nextline);
            echo 
'1LINE ' $line '<br />'// TESTING
    
$code substr($code,$nextline,$codelen);
            echo 
'1CODE ' $code '<br />'// TESTING
    
$nextline strpos($code,'<br />');
            echo 
'NEXTLINE ' $nextline '<br />'// TESTING


added the $codelen and changed the substr in the loop for the $code
Reply With Quote
  #3  
Old 02-11-2008, 07:35 AM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Don't forget to use trim() if you opt to use the first piece of code Tefra gave.
Reply With Quote
  #4  
Old 02-11-2008, 07:22 PM
MrApples MrApples is offline
 
Join Date: Aug 2007
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You do realize all you did was make the code longer right...

It was actually a stupid mistake of mine, I forgot to put a +6 after the second substring start position. So <br /> became the 0 position in the string, which is the same as false, which exited the loop ( <br /> is 6 char long ).

Thanks for the explode tip, I didn't know about that.
Reply With Quote
  #5  
Old 02-12-2008, 12:36 AM
cheesegrits's Avatar
cheesegrits cheesegrits is offline
 
Join Date: May 2006
Posts: 500
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

By the look of the code on this thread and the other one we were talking on, I'm guessing you come from a C programming background. You can use a lot of C style techniques in PHP (without the need for all that nasty malloc'ing!), but PHP provides a lot of much easier ways round a lot of common tasks.

Tefra already pointed out using 'explode' to chunk up a string into an array. You might also want to look at some of the other array functions, like array_pop.

As an example, a common requirement is to separate a filename from a directory path. Instead of doing it the C way with a tail recursive function doing substr's by steam, you would do it something like this:

Code:
$dir = '/some/path/to/a/file.txt';
$path_array = explode('/',$dir);
$file = array_pop($path_array);
$path = implode('/',$path_array);
... so $file now contains 'file.txt' and $path is '/some/path/to/a'.

You might want to consider recoding the stuff you posted on that other thread using arrays rather than doing string manipulation, if nothing else as an exercize in getting used to The Tao of PHP. PHP makes that kind of tokenizing and stack processing soooo easy ... I actually break out in hives whenever I have to actually do any serious C/C++ coding these days.

And just in case you didn't know, getting function descriptions is as easy as ...

http://www.php.net/array_pop

... or whatever function you need.

-- hugh
Reply With Quote
  #6  
Old 02-12-2008, 11:28 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Maybe not the best example, for filename processing PHP also provide functions like basename() etc..
Reply With Quote
  #7  
Old 02-12-2008, 02:07 PM
cheesegrits's Avatar
cheesegrits cheesegrits is offline
 
Join Date: May 2006
Posts: 500
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well yeah, but for starters basename() doesn't give you the separated path, you have to call dirname() as well, and doing it this way you get the tokenized path array, which is often useful.

And it was just an example.

-- hugh
Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:11 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.06216 seconds
  • Memory Usage 2,230KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete