1

I have this specific code that requires to be converted to preg_replace_callback however what it returns is the actual variable and not the value of the variable. Here is my code:

preg_replace:

$strValue = preg_replace("/(MYSQL_DATA::)([a-zA-Z0-9_]*)([^(::)]*)(::)/",
          "\$rowData['\\2']", $this -> m_arrColumnValues[$key]);

here is the conversion to preg_replace_callback:

$strValue = preg_replace_callback("/(MYSQL_DATA::)([a-zA-Z0-9_]*)([^(::)]*)(::)/",
                    function ($matches) { return "\$rowData[$matches[2]]"; }, $this -> m_arrColumnValues[$key]);

Here is the output:

$rowData[fportTitle] 

It is supposed to be from the result of this $rowData = mysqli_fetch_array($rsData) but it displays the actual variable and not its supposed value.

Thank you in advance StackOverflow community.

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
  • FYI: `[^(::)]*` means 0 or more times any character that is not `(` or `:` or `)`, it's a nonsense to duplicate `:` in a character class. – Toto Mar 11 '20 at 09:34

1 Answers1

0

I was able to get the data that I need. Since preg_replace_callback calls a function, a variable outside of will not be accessed therefore I am not getting the data I expected. So here is an update to my code:

$strValue = preg_replace_callback("/(MYSQL_DATA::)([a-zA-Z0-9_]*)([^(::)]*)(::)/",
                        function ($matches) use ($rowData) { return $rowData[$matches[2]]; }, $this -> m_arrColumnValues[$key]);

On the function, I utilized the use to access the variable outside the function.