PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] Why is my PHP class method returning nothing when I try to use values taken from using preg_replace inside foreach without giving errors?

 November 20, 2022     arrays, php, preg-replace, return     No comments   

Issue

I'm trying to build an array with a few variations of some data, when using preg_replace and echoing out values, it works like normal, but if I try to store those values in an array, the function simply returns nothing, and gives no error.

Here's a piece of the code I'm using, it's inside a class.

    $actions = array();

    foreach($controllers as $ck => $cv) {

        $cvar = 'br'.$ck.'actions';

        foreach(self::$$cvar as $key => $value) { 
            
            if ($key != $value) {
                $actions[$value] = $key;
            
                if( preg_match('/[A-Z]/',$key)!==0 || preg_match('/[A-Z]/',$value)!==0 )
                {               
                    $key2=strtolower(preg_replace('/(?<=\\w)([A-Z])/','-\\1',$key)); 
                    $value2=strtolower(preg_replace('/(?<=\\w)([A-Z])/','-\\1',$value)); 
                    $actions[$value2] = $key2; // Everything works except for this line, if I comment it, it works, if I don't, it simply doesn't return even an error.
                }   

            }
        } 
    }
    return $actions;

What is causing this weird behaviour? It should be able to add to the array like normal, but it doesn't...


Solution

So, I found out what was the problem:

In the arrays of values being used to create the $actions array (The $br(controller)actions array) there were some values that accidentally had some diacritics on them (Like the word "Demonstração"), which when going through the preg_replace function, became some of those black question mark boxes (�� Replacement Characters), which then were fed as array keys by the function and somehow made the function return empty instead of the array. Printing the values showed that the function still ran through all the loop iterations, but the array simply didn't return properly with those values on it.

I fixed it by removing the diacritics, then the method ran smooth as it should.



Answered By - Miguel Vieira
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing