SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Issue with False preg_match

    Why does this code always go to the TRUE branch regardless of the value I put in for $body?

    PHP Code:
    <?php
        $body 
    'xxx';

        if (
    preg_match('~([0-9-]+)~'$body) !== false){
            echo 
    'Image Found';

        }else{
            echo 
    'Image Not Found';

        }
    ?>

    According to the PHP Manual...

    preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
    Warning

    This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

    So 'xxx' should return '0' and since I am using '!==' shouldn't things resolve to the ELSE branch??

    Thanks,


    Debbie

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    No, as FALSE in this scenario means your regex is invalid. You want to check against 0 for no match. So if you use !== 0, you will get the expected result.

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you have to watch out when using the strict comparisons.

    PHP Code:
    == true // true
    === true // false
    == false // true
    === false // false 
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    No, as FALSE in this scenario means your regex is invalid. You want to check against 0 for no match. So if you use !== 0, you will get the expected result.
    cpradio,

    Glad to catch you online, because this problem came about when I was modifying some code that you gave me that I apparently didn't understand.

    Rewinding...

    You helped me with this code...
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER) !== false){
            
    // Loop through array.
            
    foreach ($matches as $match){
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        } 

    Preferring a "balanced" IF-THEN-ELSE, I made this tweak...
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER) !== false){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    But apparently this code doesn't do what I thought or intended, right?

    So, some questions...

    1.) I suppose having the !== false is good because it catches when my preg_match_all fails, right??

    2.) If I want to keep the preg_match_all, then how do I make my IF-THEN-ELSE still work?

    Or is that not needed?

    Thanks,


    Debbie

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    1.) I suppose having the !== false is good because it catches when my preg_match_all fails, right??
    Yes, granted it should never fail since it isn't generated by code and is hard coded, but nonetheless, I usually keep an if statement around it (in case someone changes the regex for example).

    Quote Originally Posted by DoubleDee View Post
    2.) If I want to keep the preg_match_all, then how do I make my IF-THEN-ELSE still work?

    Or is that not needed?
    That entirely depends on "what will you do if there are no matches?" If you plan to do nothing, then you don't need an else, as it won't provide anything useful to you or your users (whereas, the if/else for the preg_match_all is necessary so you know the regular expression failed entirely and it didn't search for any cross-links.

    If you desperately wanted an else, you would put it inside your if block and check the sizeof($matches) != 0, then run your foreach, else do something else.

  6. #6
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it's not really working the way you think it is working. preg_match_all !== false doesn't check to see if there are matches. It just makes sure there wasn't an error when calling the function. The function creates $matches = array(); to store any results. If there are no results, $matches will be an empty array. I think you're trying to do something like this:
    PHP Code:
        // Find all Cross-Links in Article. 
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER) !== false){ 
            if (
    count($matches)) { // Matches found
                // Loop through array. 
                
    foreach ($matches as $match){ 
                    
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body); 
                }
            } else { 
    // No matches found
                // do something
            
    }

        } 
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    Yeah, it's not really working the way you think it is working. preg_match_all !== false doesn't check to see if there are matches. It just makes sure there wasn't an error when calling the function.
    It was working exactly the way I intended it But I didn't explain it well it enough in my prior thread, the great thing is the foreach will just exit because $matches would be an empty array on its own, which is why you don't need the inner IF statement UNLESS you want to do something if there are 0 matches found. Of course, you were probably writing up your response as I was writing mine

  8. #8
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Lol I was talking to her. I guess you responded before I finished typing.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    Lol I was talking to her. I guess you responded before I finished typing.
    I would say "nana boo boo, I beat you" but that just seems too immature Good to see you around @kduv ;

  10. #10
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I usually check out the forums every day or so, but lately you and cups have been getting to the posts before me and I haven't had much of anything to add. I never really feel right posting a "yeah me too" reply. It just seems like a waste.

    But yeah, I do agree with you. Unless she wants to do something when no matches are found, I'd leave it like it was as the foreach will take care of it.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  11. #11
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Yes, granted it should never fail since it isn't generated by code and is hard coded, but nonetheless, I usually keep an if statement around it (in case someone changes the regex for example).



    That entirely depends on "what will you do if there are no matches?" If you plan to do nothing, then you don't need an else, as it won't provide anything useful to you or your users (whereas, the if/else for the preg_match_all is necessary so you know the regular expression failed entirely and it didn't search for any cross-links.

    If you desperately wanted an else, you would put it inside your if block and check the sizeof($matches) != 0, then run your foreach, else do something else.
    If the syntax of my Regex was invalid, since it is also hard-coded, wouldn't I get a compile error or a runtime error so I'd know my Regex is syntactically invalid?

    On the code you helped me out with last week, I had this...
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER) !== false){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    I guess that is wrong, right?


    I tried re-writing things to look like this, but I'm still not sure if it is necessary...
    PHP Code:
        if (($result preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)) === FALSE){
            
    // Bad Pattern.
                // Now what do i do???
                        
        
    }else if ($result){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    Does that look better?


    Off Topic:

    Sorry, I haven't eaten since yesterday, and my brain is fading quickly before supper, but since you are here, I didn't want to miss your help!!



    Before this whole topic came up, I would have normally just coded things this way...
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    Isn't this last way good enough??

    Thanks,


    Debbie

  12. #12
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    Yeah, it's not really working the way you think it is working. preg_match_all !== false doesn't check to see if there are matches. It just makes sure there wasn't an error when calling the function. The function creates $matches = array(); to store any results. If there are no results, $matches will be an empty array. I think you're trying to do something like this:
    PHP Code:
        // Find all Cross-Links in Article. 
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER) !== false){ 
            if (
    count($matches)) { // Matches found
                // Loop through array. 
                
    foreach ($matches as $match){ 
                    
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body); 
                }
            } else { 
    // No matches found
                // do something
            
    }

        } 

    I wasn't thinking that at all, but you bring up another interesting point!!

    Hmm...


    Debbie

  13. #13
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I don't think it will return false for an invalid regex syntax. That will most likely just return no results. When it says "returns false on error" it means something like setting the offset higher than the subject length.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  14. #14
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    cpradio (and kduv), I am ready to PASS OUT from starvation and working 10 hours straight...

    If you get a chance later tonight, please let me know what you think about this related thread...

    How to display Images for an Article

    Off to supper for now...

    Thanks, and I'll respond when I can!!!


    Debbie

  15. #15
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    If the syntax of my Regex was invalid, since it is also hard-coded, wouldn't I get a compile error or a runtime error so I'd know my Regex is syntactically invalid?
    I don't think it would because at the JIT time, it is just a string, it isn't until preg_match_all tries to use it, that is validates it and executes the regular expression. Since the method only returns true or false, my guess is you may get an error/notice/warning, but if you have those disabled in Production from being displayed (like you should), the true/false is all you can depend on.

    Quote Originally Posted by DoubleDee View Post
    Before this whole topic came up, I would have normally just coded things this way...
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    Isn't this last way good enough??
    Yes, that is acceptable, EXCEPT your comments in the ELSE are incorrect, it doesn't mean Cross-Links Not Found, it means, the match statement failed because someone changed the regex so that it wasn't valid syntax (such as using (.?*) instead of (.*?) (my dyslexia always causes me to type that one wrong; and I do realize your regular expression doesn't currently contain those characters).

    As for this one
    Quote Originally Posted by DoubleDee View Post
    PHP Code:
        if (($result preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)) === FALSE){ 
            
    // Bad Pattern. 
                // Now what do i do??? 
                         
        
    }else if ($result){ 
            
    // Cross-Links Found. 

            // Loop through array. 
            
    foreach ($matches as $match){ 
                
    // Pass sub-array to function which creates a new Article URL, 
                // and then replaces each Cross-Link "placeholder" with the new link. 
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body); 
            } 

        }else{ 
            
    // Cross-Links Not Found. 
            // Do nothing... 
        

    The ELSE will never run, as your first condition already handles a FALSE $result, remember 0 matches is still a TRUE result from preg_match_all, so you would have to check within your ELSE IF that count($matches) or sizeof($matches) is NOT 0 to detect NO MATCHES.

    Again, I ONLY recommend doing that ELSE, if you plan to do something about NO MATCHES being found. If you don't plan to do anything, leave it as

    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // preg_match_all failed to run
            // Do nothing...
        


  16. #16
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by kduv View Post
    I usually check out the forums every day or so, but lately you and cups have been getting to the posts before me and I haven't had much of anything to add. I never really feel right posting a "yeah me too" reply. It just seems like a waste.

    But yeah, I do agree with you. Unless she wants to do something when no matches are found, I'd leave it like it was as the foreach will take care of it.
    Actually, I've ran into several threads where you beat me And must say, I really do appreciate your responses.

  17. #17
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Actually, this function WILL return the number of results, or boolean false on error.

    So this will work:
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    Keith
    Freelance web developer
    http://www.duvalltech.com/

  18. #18
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    I don't think it will return false for an invalid regex syntax. That will most likely just return no results. When it says "returns false on error" it means something like setting the offset higher than the subject length.
    That very well could be, the manual is vague on that aspect, and I guess we could denote that as being the case due to version 5.3.6 providing "Returns FALSE if offset is higher than subject length.", but I digress there are likely other scenarios (I hope) that would return false (but maybe there aren't).

    Quote Originally Posted by kduv View Post
    Actually, this function WILL return the number of results, or boolean false on error.

    So this will work:
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    Apart from the comment in the ELSE still being wrong, I agree. As the else does not mean Cross-Links Not Found, it means, preg_match_all did not run because it had an error.

  19. #19
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Apart from the comment in the ELSE still being wrong, I agree. As the else does not mean Cross-Links Not Found, it means, preg_match_all did not run because it had an error.
    It would still fire the ELSE if no matches are found as the function would return a 0 instead of a boolean false (or positive int in the case of one or more matches). The only downside would be that the ELSE would fire if there are no matches and if there is an error since you're only testing for true/false and not boolean true/false.

    She may or may not be interested in differentiating between the two, but I personally like to make sure errors are handled correctly, and I of course differentiate between an error and an empty result set.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  20. #20
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    It would still fire the ELSE if no matches are found as the function would return a 0 instead of a boolean false (or positive int in the case of one or more matches). The only downside would be that the ELSE would fire if there are no matches and if there is an error since you're only testing for true/false and not boolean true/false.

    She may or may not be interested in differentiating between the two, but I personally like to make sure errors are handled correctly, and I of course differentiate between an error and an empty result set.
    Bah, I mis-read the manual. I thought it only returned TRUE or FALSE, my bad. You are correct.

  21. #21
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    cpradio and kduv,

    I appreciate you trying to help, but the two of you have me thoroughly confused?!


    Debbie

  22. #22
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    cpradio and kduv,

    I appreciate you trying to help, but the two of you have me thoroughly confused?!


    Debbie
    Yeah, that can happen. So to summarize:

    In short, the code
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)){
            
    // Cross-Links Found.

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.
            // Do nothing...
        

    will do what you want. The caveat of it (and I made this mistake), is that I assumed the ELSE in this case would only apply for a failed preg_match_all execution. That isn't true. It will also apply when 0 matches are found. So the ELSE takes on two meanings, failed preg_match_all OR 0 matches.

    To separate them out (if you want to), I prefer the following approach
    PHP Code:
        // Find all Cross-Links in Article.
        
    if (preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER) !== false){
            if (
    count($matches) != 0)
            {
               
    // Cross-Links Found.

               // Loop through array.
               
    foreach ($matches as $match){
                   
    // Pass sub-array to function which creates a new Article URL,
                   // and then replaces each Cross-Link "placeholder" with the new link.
                   
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
               }
            }else{
               
    // No matches found, do nothing? show a message?
            
    }
        }else{
            
    // preg_match_all failed
            // let the user know the regular expression did not run properly
        

    This will let you have separate messages/execution paths for no matches versus the preg_match_all failed to execute.

  23. #23
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here is what the PHP Manual says for preg_match...

    preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
    Warning

    This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

    I am assuming the same logic applies to preg_match_all?!


    ---------
    Based on this, here is my best interpretation of what this means, and how I should tweak my former code...

    PHP Code:
        // ************************************
        // Check for Cross-Links in Article.    *
        // ************************************
        
    if (($result preg_match_all('/\\{url=([a-zA-Z0-9-]+)\\}/'$body$matchesPREG_SET_ORDER)) === FALSE){
            
    // Bad Pattern.  (Regex returned 'FALSE')
            // Now what do i do???

        
    }elseif ($result == 1){
            
    // Cross-Links Found.  (Regex returned '1' meaning at least one Cross-link was found.)

            // Loop through array.
            
    foreach ($matches as $match){
                
    // Pass sub-array to function which creates a new Article URL,
                // and then replaces each Cross-Link "placeholder" with the new link.
                
    $body str_replace($match[0], generateArticleCrossLink($dbc$match), $body);
            }

        }else{
            
    // Cross-Links Not Found.  (Regex returned '0' meaning that no Cross-Links were found.)
            // Do nothing...
                        
        
    }//End of CHECK FOR CROSS-LINKS IN ARTICLE 

    I'm not sure what to do if I get a 'FALSE'...

    And while my ELSE does nothing, 'I' like it because it helps me to complete the last "path" that my code could take!!

    -------
    Back to my earlier comment, since I am NOT using offsets, and my code is hard-coded, I am wondering if checking for 'FALSE' is maybe a bit extreme??

    Anyways, if you guys could lasso all of the different directions this thread has went on, and determine what is "right" - and more so what is "wrong" - that would be great. (I'm a newbie and don't want to hang myself!!)

    Thanks as always!!


    Debbie

  24. #24
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    cpradio,

    It just occurred to me that my version of MAMP runs PHP v5.2.6 so maybe all of this fuss over checking for 'FALSE' was for not?!

    The last set of code I posted above seems to be working fine, except I cannot figure out how to throw a 'FALSE'. But, as just mentioned, maybe that isn't possible with my set-up...

    I think I'll stick with the last set of code because in my production environment it will probably be good to have.


    Debbie

  25. #25
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    cpradio,

    It just occurred to me that my version of MAMP runs PHP v5.2.6 so maybe all of this fuss over checking for 'FALSE' was for not?!

    The last set of code I posted above seems to be working fine, except I cannot figure out how to throw a 'FALSE'. But, as just mentioned, maybe that isn't possible with my set-up...

    I think I'll stick with the last set of code because in my production environment it will probably be good to have.


    Debbie
    I'd argue, anything that accomplishes what you are wanting to happen, is a good solution

    The PHP version you are on, may make a difference to how the code is written and without having that version available to me, I couldn't begin to tell you which versions of our code are capable of running on 5.2.6

    I don't see anything wrong with the lastest code you posted, so if it runs locally for you and on your server, I say use it


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
  •