vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Php coding question (https://vborg.vbsupport.ru/showthread.php?t=282632)

Chris8 05-08-2012 06:29 PM

Php coding question
 
I'm messing with bbcodes and I have a little problem here.
What will be output of this:

Code:

preg_replace('/\{option(\d+)\}/e', "'%' . (intval('\\1')+1) . '\$s'", $bbcode['html']);
It supposed to replace {option1} {option2} {option3} and so on but to what exactly?

The default {option} is replaced with %2$s the above must have something in common with %2$s but I'm not sure how it's transformed.

Please help me to resolve this problem. :)

kh99 05-08-2012 06:38 PM

The matched text that corresponds to parts of the pattern enclosed in parens can be used in the replacement as \1 for the first set of parens, \2 for the second, etc. So the (\d+) in the pattern matches a string of one or more digits (i.e. an integer), and it's used in the replacement as '\\1' (why it needs an extra backslash is another story - it might be best if you look at this page for the description of the 'e' modifier http://www.php.net/manual/en/referen....modifiers.php).


Edit: reading your post again, maybe I misunderstood? I don't know why {option} would be matched at all by that pattern.

Chris8 05-09-2012 03:45 PM

I still don't understand. ^^' Yeah, it's not going to match but to replace {option1} {option2} and so on with something and I'm looking for to get to know what is this something. I'm almost sure it is %2$s with appended number, what I'm not sure is how the numbers are appended. For example would it be %3$s, %4$s etc? Probably no, %21$s %23$s ? or maybe %2+1$s, %2+2$s? I'm not a coder so it's difficult for me to interpret this. The question is what will be {option1} {option2} {option3} and so on transformed to after the replacement.

Hire is the whole code:

PHP Code:

!$this->is_wysiwyg())
{
    foreach (
$this->tag_list['option'] AS $tag => $bbcode)
    {
        if (
strpos($bbcode['html'], '{option1}') !== false)
        {
            unset(
$this->tag_list['option']["$tag"]['html']);
            
$this->tag_list['option']["$tag"]['replace'] = preg_replace('/\{option(\d+)\}/e'"'%' . (intval('\\1')+1) . '\$s'"$bbcode['html']);
            
$this->tag_list['option']["$tag"]['callback'] = 'handle_external';
            
$this->tag_list['option']["$tag"]['external_callback'] = 'handle_bbcode_multioption';
        }
    }



kh99 05-09-2012 03:50 PM

It looks to me like {option0} is replaced by %1$s, {option1} by %2$s, etc.

Chris8 05-09-2012 07:10 PM

I got it working. I've used plain {optionx} instead of %3$s %4$s etc,
when I try to replace it with variables like: %3$s %4$s, nothing is displaying when I test it in production, only %2$s seems to work but it displays all options together and duplicates them.

Thank you for trying anyway, I appreciate it. :)


All times are GMT. The time now is 04:43 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.01048 seconds
  • Memory Usage 1,729KB
  • 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
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)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