Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    PHP question.

    Simplified, this is the problem I have:
    PHP Code:
    $sometext='Hey!  This is clause %n; this is clause %n.  
    Be clause %n.  I think (clause %n) that you are a fool
    (clause %n) and you deserve to die (clause %n).'

    Alright, now that's just a simplified example and not at all the application (so don't get hung up on particulars). What I want to do is replace %n in the string '$sometext' with the results of a function call (pretend it returns an increasing integer: 1, 2, 3...).
    PHP Code:
    function increase(){static $x=1; return $x++;}
    //wouldn't work, but this is the idea:
    str_replace('%n',increase(),$sometext);
    /*Returns: 
    Hey!  This is clause 1; this is clause 1.   
    Be clause 1.  I think (clause 1) that you are a fool 
    (clause 1) and you deserve to die (clause 1).

    Should return:  
    Hey!  This is clause 1; this is clause 2.   
    Be clause 3.  I think (clause 4) that you are a fool 
    (clause 5) and you deserve to die (clause 6).*/ 
    So how do I do a sort of str_replace() that replaces the term with a function call (each time)? Would I have to use some sort of RegExp or am I barking up the wrong tree? Any suggestions appreciated!
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    You wouldn't need a regexp, you'd probably be better off with some sort of splitting+reconstructing+iterating. Something like:
    PHP Code:
    $splitStr explode'%n'$str );

    $reconstructedStr $splitStr[0];
    foreach ( 
    $splitStr as $str )
    {
        if ( 
    $reconstructedStr == $str )
            continue; 
    // skip the first one

        
    $reconstructedStr .= functionCall() . $str;


  4. #3
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Remember to globalize your function variable as well.

    PHP Code:
    function test()
    {
        @
    $x++;
        return 
    $x;
    }


    echo 
    test().", ".test().", ".test();

    //Returns: 1,1,1

    ######################

    function test()
    {
        global 
    $x;
        
    $x++;
        return 
    $x;
    }


    echo 
    test().", ".test().", ".test()

    //Returns: 1,2,3 
    S. Rosland

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Actually, I believe the appropriate way to deal with this is a static variable, not a global one. I believe PHP4 supports static variables. So, instead of:
    PHP Code:
    function test()
    {
        global 
    $x;
        
    $x++;
        return 
    $x;

    You would do:
    PHP Code:
    function test()
    {
        static 
    $x 0;
        
    $x++;
        return 
    $x;

    This avoids polluting the global namespace and accidentally modifying the variable elsewhere, since the scope of the variable is still local to the function, but the value persists between function calls.

    Correct me, of course, if this doesn't work in PHP4; however, the page on php.net appears to indicate it does.

  6. #5
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Sounds good to me
    S. Rosland

  7. #6
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Wow thanks everyone! Shadowfiend... that looks like just the trick. Thanks.

    Also, that's correct, the best practice is to use the static variable...

    Rosland, I've never really seen that @ sign used in PHP... is that how to properly declare a variable and use it in the same line, or what? (I'm referring to this:
    PHP Code:
    function test()
    {
        @
    $x++;
        return 
    $x;

    )
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  8. #7
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    That worked, Shadowfiend!

    I turned your code into a function that accepts a string and a function name (string), runs your code, and returns the reconstructed string.
    PHP Code:
    <?php
    function increase(){static $x=1; return $x++;}

    function 
    str_replace_dynamic($needle$function$haystack){
      
    $splitStr explode($needle$haystack);
      
    $reconstructedStr $splitStr[0];
      foreach ( 
    $splitStr as $str )
      {
       if(
    $reconstructedStr==$str) continue; //skip the first one
       
    $reconstructedStr .= $function() . $str;
      } 
      return 
    $reconstructedStr;
    }
    $sometext='Hey!  This is clause %n; this is clause %n.  
    Be clause %n.  I think (clause %n) that you are a fool
    (clause %n) and you deserve to die (clause %n).'
    ;

    print 
    str_replace_dynamic('%n','increase',$sometext);
    ?>
    Returns:
    Hey! This is clause 1; this is clause 2. Be clause 3. I think (clause 4) that you are a fool (clause 5) and you deserve to die (clause 6).
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  9. #8
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Quote Originally Posted by straight_up
    Rosland, I've never really seen that @ sign used in PHP... is that how to properly declare a variable and use it in the same line, or what?[/php])
    No, it's just to avoid PHP throwing 'undefined variable' errors in your face. (Depending on what level of error reporting you have defined in your php.ini file.
    If you add ~E_NOTICE to error_reporting, you will always supress these kinds of errors)

    If you define the variable beforehand (like $x=1; or static $x; ), you won't run in to the problem.
    However, if you iterate a veriable that has yet not recieved any value ($x++; which is fully legal) PHP will protest, as it has yet not seen this variable defined. Same will happen if you have an if() clause where one of the compared values is an unset variable.

    The '@' sign, is just an error supressor.
    S. Rosland

  10. #9
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Quote Originally Posted by rosland
    No, it's just to avoid PHP throwing 'undefined variable' errors in your face....
    The '@' sign, is just an error supressor.
    Thanks!
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  11. #10
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Smoothness :-)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:55 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com