The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
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:
|
#2
|
||||
|
||||
you basically want to echo each line right ?
PHP Code:
PHP Code:
added the $codelen and changed the substr in the loop for the $code |
#3
|
|||
|
|||
Don't forget to use trim() if you opt to use the first piece of code Tefra gave.
|
#4
|
|||
|
|||
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. |
#5
|
||||
|
||||
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); 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 |
#6
|
|||
|
|||
Maybe not the best example, for filename processing PHP also provide functions like basename() etc..
|
#7
|
||||
|
||||
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 |
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|