Issue with False preg_match

cpradio and kduv,

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

Debbie

Yeah, that can happen. So to summarize:

In short, the code

    // Find all Cross-Links in Article.
    if (preg_match_all('/\\\\{url=([a-zA-Z0-9-]+)\\\\}/', $body, $matches, PREG_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

    // Find all Cross-Links in Article.
    if (preg_match_all('/\\\\{url=([a-zA-Z0-9-]+)\\\\}/', $body, $matches, PREG_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.

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ā€¦


	// ************************************
	// Check for Cross-Links in Article.	*
	// ************************************
	if (($result = preg_match_all('/\\\\{url=([a-zA-Z0-9-]+)\\\\}/', $body, $matches, PREG_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

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 :wink:

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 :slight_smile: