Jump to content
Sign in to follow this  
Andy_K

preg_replace in for loop issues

Recommended Posts

Hello,

 

I've currently got an issue where when using preg_replace in a forloop, it duplicates code:

 

Example.gif

 

The code I'm using is:

            function GenerateSwitchDiagram($Code) {
                $Html           = "";
                $SwitchCount    = preg_match_all('/\\[Switch(.|\n)*?\[EndSwitch]/', $Code, $SwitchContent);

                for($Switch = 0; $Switch < $SwitchCount; $Switch++) {
                    preg_match_all('/\[Switch\s*([^\]]*)\s*\]/', $SwitchContent[0][$Switch], $SwitchTags, PREG_SET_ORDER);
                    preg_match_all('/([^=\s]+)="([^"]+)"/', $SwitchTags[0][1], $SwitchInfo, PREG_SET_ORDER);

                    $Name = $SwitchInfo[0][2];
                    $Size = $SwitchInfo[1][2];

                    $Html .= '<div class="SwitchContainer SwitchSize_' . $Size . '">';
                    $Html .= '<div class="SwitchName">' . $Name . '</div>';

                    for($Port = 1; $Port <= $Size; $Port++) {
                        if(preg_match_all('/\[Port' . $Port . ' \s*([^\]]*)\s*\]/', $SwitchContent[0][$Switch], ${'Port' . $Port}, PREG_SET_ORDER) == 1) {
                            preg_match_all('/([^=\s]+)="([^"]+)"/', ${'Port' . $Port}[0][1], $PortInfo, PREG_SET_ORDER);

                            $Comment = $PortInfo[0][2];
                            $Mode    = $PortInfo[1][2];
                            $VLANS   = $PortInfo[2][2];

                            $Html .= '<div class="SwitchPort PortLive">';
                            $Html .= $Port;
                            $Html .= '<span class="SwitchComment"><strong>Comment:</strong> ' . $Comment . '<br /><strong>Mode:</strong> ' . $Mode . '<br /><strong>VLAN(s):</strong> ' . $VLANS . '</span>';
                            $Html .= '</div>';
                        } else {
                            $Html .= '<div class="SwitchPort PortDown">';
                            $Html .= $Port;
                            $Html .= '</div>';
                        }
                    }
                    
                    $Html .= '</div>';
                    $Code = preg_replace('/\[Switch Name="' . $Name . '" Size="' . $Size . '"](.|\n)*?\[EndSwitch]/', $Html, $Code);
                }
                
                return $Code;
            }

I've had a look and it looks like I should be using preg_replace_callback() rather than preg_replace, but I need to replace dynamic strings, not static so I'm not sure how I would replace the following line to use preg_replace_callback():

$Code = preg_replace('/\[Switch Name="' . $Name . '" Size="' . $Size . '"](.|\n)*?\[EndSwitch]/', $Html, $Code);

Thanks,

Andy

Share this post


Link to post
Share on other sites

I can't seem to edit my last post, but I've resolved the issue. - Bad placement of the $Html variable. Should have been under the first for loop rather than above it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.