Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2004, 08:54 PM
KW802's Avatar
KW802 KW802 is offline
 
Join Date: Jul 2003
Location: A galaxy far, far away...
Posts: 1,450
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default PHP string question (getting part of)...

Hopefully a quick question..... :nervous:

What's the easiest way of getting a section of a string based upon an X specified delimiter?

To show what I'm after, let's say that I have two strings that always contain the same number of delimters such as "X_Y_Z" and "A_B_C_D". The length of each of the parts might change (eg: "AAA_BB_C_DDDDD") but the number of delimeters don't. Now if a string is concatenated at the end of it such as "X_Y_Z_123" then I want to get "_123" including the underscore. If the string is "A_B_C_D_123_456" then I want to get "_123_456" out of it. If the string is "X_Y_Z" then I want a blank value. In the first string I would want to get from the 4th part onward and in the second string I want to get from the 5th part onward.

Any thoughts? I know that I could turn the string into an array based upon the delimiter and then just say get from the X value onward if X isn't blank but then I'd have to concatenate all of the array values after X back together again. Hopefully I'm just thinking about it too much and that there's a quick way of getting the results.

Thanks,
Reply With Quote
  #2  
Old 09-30-2004, 02:38 AM
Modin Modin is offline
 
Join Date: Jun 2004
Posts: 162
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Easiest would probably be to split the string into it's parts, then rejoin them when you got what you want
PHP Code:
$str "AA_B_C_DDD_123_456"
$str_array explode("_"$str);
for(
$i 0$i 4$i++)
{
    
array_shift($str_array);
}
$newstr "_".implode("_"$str_array); 
with this $newstr will end up being _123_456

hope this gets you on your way,

Modin
Reply With Quote
  #3  
Old 09-30-2004, 12:32 PM
KW802's Avatar
KW802 KW802 is offline
 
Join Date: Jul 2003
Location: A galaxy far, far away...
Posts: 1,450
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Modin
hope this gets you on your way,
Yes that does, thank you! What I'm seeing in my head is to turn that chunk of code into a function with the "4" in "$i < 4" being a variable that I can pass in so that it'll handle when I need from the X part onward or the Y part onward.

Thanks!

Kevin
Reply With Quote
  #4  
Old 09-30-2004, 01:06 PM
buro9 buro9 is offline
 
Join Date: Feb 2002
Location: London, UK
Posts: 585
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by KW802
Yes that does, thank you! What I'm seeing in my head is to turn that chunk of code into a function with the "4" in "$i < 4" being a variable that I can pass in so that it'll handle when I need from the X part onward or the Y part onward.

Thanks!

Kevin
If your values between the delimiters are only ever alphanumeric you could use a regular expression with preg_match and fetch the last match.

preg's are fast and this would avoid a new array being created and the loop.

But for that to work you'd need to know what characters could come between the delimiters.
Reply With Quote
  #5  
Old 09-30-2004, 08:16 PM
KW802's Avatar
KW802 KW802 is offline
 
Join Date: Jul 2003
Location: A galaxy far, far away...
Posts: 1,450
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by buro9
If your values between the delimiters are only ever alphanumeric you could use a regular expression with preg_match and fetch the last match.

preg's are fast and this would avoid a new array being created and the loop.

But for that to work you'd need to know what characters could come between the delimiters.
buro9,

I hadn't thought of using a preg. I may have to do the array trick though because the values may be anything. What I'm working on is I did a small hack job to the BV Shoutbox Full code so that I could easily implement it in vBa CMPS but my changes were quick & dirty. I'm thinking that I can simplify the process so that instead of releasing a modified version of the PHP file I can instead write up instructions for the users to follow and modify the file themselves; that way the only thing I'd have to supply are the instructions and the new templates to create.

It's not high on my priority list right now but I figured I'd still put it out there in case anybody else wants it.

Thanks,
Kevin
Reply With Quote
  #6  
Old 10-03-2004, 02:21 AM
Modin Modin is offline
 
Join Date: Jun 2004
Posts: 162
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

yeah, I thought about preg as well...though I find that when you have a common delimiter explode is much easier.

And technically a new array is created when you use preg, the matches it returns are in an array. And you don't know how it's implemented in the back end, so it's very possible that many arrays are created
Reply With Quote
Reply


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 08:26 PM.


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.03678 seconds
  • Memory Usage 2,216KB
  • 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_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)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