Jump to content

preg_replace in for loop issues

Go to solution Solved by Andy_K,

Recommended Posts



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




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);



Link to post
Share on other sites
  • Solution

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.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

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.