SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    incrementing a variable in str_replace

    Hey guys,

    I am completely baffled on how to solve this one!

    I have a bb code function that I used to render some custom codes I have. One of them is "Spoiler". The spoiler allows the user to have hidden content inside their posts that's shown when a button is clicked.

    The spoiler code requires the div to have a unique ID so the button knows what div to show.

    So I have:
    $random = rand(25, 30);
    $random2 = rand(2, 10);
    $spoilerid = $random.$random2.

    $text = str_replace('[spoiler]', '<input type="button" value="Show Spoiler" onClick="Javascript : noDisp(\''.$spoilerid.'\');">
    <div id="'.$spoilerid.'" class="spoiler_content" style="display: none">', $text);

    ...
    A random number is generated and assigned to that div. Although slightly longwinded, it works for when the spoiler tag is only used once in a post, but I'm confused as to how to make it work if more than one is used.

    Like would there by a way to increment the value of spoilerid or something?

    Cheers

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    since the id only needs to be unique for the current html being output, yeah just increment a value. theres not even a need to use a random number.

    PHP Code:

    $id 
    0;

    $first_spoiler 'spoiler' $id;
    $id++;
    $second_spoiler 'spoiler' $id;
    $id++;

    //etc... 

    or if you want a function, you could make nice use of static variables here

    PHP Code:
    function spoiler_id() {
        static 
    $id;
        
    $id++;
        return 
    'spoiler' $id;
    }


    echo 
    spoiler_id();
    echo 
    spoiler_id();
    echo 
    spoiler_id(); 

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you tell me what this does:

    onClick="Javascript:

    ??
    Although slightly longwinded, it works for when the spoiler tag is only used once in a post, but I'm confused as to how to make it work if more than one is used.
    Try this:
    PHP Code:
    $str '[spoiler]faadfasdfsa[spoiler]adfasdfdsf[spoiler]';
    $arr explode('[spoiler]'$str);

    $spoilerID 1.5;

    $count count($arr);
    if(
    $count 1)
    {
        for(
    $i=0$i $count-1$i++)
        {
            echo 
    $arr[$i] . "**div id={$spoilerID}**spoiler text**/div**" ;
            
    $spoilerID++;
        }
        if(
    $arr[$count-1] !== '')
            echo 
    $arr[$count 1];

    Replace the **'s with the appropriate symbols '<' or '>'.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, but I should have explained it better.

    What it's looking for is the term [spoiler] in a piece of text - is there a way to have some sorta for loop like:

    for (search for the term) {
    do this
    increment value
    }

    ?

    Thanks

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhmmm...isn't that what the code I posted does?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •