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.

1
  • FYI: [^(::)]* means 0 or more times any character that is not ( or : or ), it's a nonsense to duplicate : in a character class. Commented Mar 11, 2020 at 9:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.