vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Troubles with substr (PHP) (https://vborg.vbsupport.ru/showthread.php?t=170174)

MrApples 02-10-2008 08:32 PM

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?

Tefra 02-10-2008 09:42 PM

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

Opserty 02-11-2008 07:35 AM

Don't forget to use trim() if you opt to use the first piece of code Tefra gave.

MrApples 02-11-2008 07:22 PM

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.

cheesegrits 02-12-2008 12:36 AM

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

Marco van Herwaarden 02-12-2008 11:28 AM

Maybe not the best example, for filename processing PHP also provide functions like basename() etc..

cheesegrits 02-12-2008 02:07 PM

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


All times are GMT. The time now is 03:21 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01216 seconds
  • Memory Usage 1,743KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete