Need help with Syntax in PHP script with parameters

rleyba828
rleyba828 used Ask the Experts™
on
Hi Team,

  Just need some help with PHP syntax where I have to do some variable substitution in my PHP statement where i have a web page of network device that has multiple columns and I need to construct an href tag.

I need help crafting a function where the value returned for one of the parameters would be the SWITCH name on that HTML row, combined with the PORT name but without the Fa or Gi prefixes, and wildcard characters (*), interspersed between them.

Example:

Row         SWITCH      PORT       output
1              Cisco-A   Fa0/1/1    *Cisco-A*0/1/1*
2              Cisco-B   Gi1/1       *Cisco-B*1/1*
3              Cisco-C   Gi1/4       *Cisco-C*1/4*
4              Cisco-C   Fa0/4      *Cisco-C*0/4*

   function PORT__renderCell(&$record){
    return '<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*'.$record->val('SWITCH').'*$record->val('PORT')* ">'.$record->val('PORT').'</a>';
}

Open in new window


My syntax above is definitely wrong as my concatenation syntax is all wrong and I don't know how to extract only the right string of the PORT variable up to the pt excluding Gi or FA.  I have the table tags worked out so that's fine.  I just need help rewriting the my function above  to convert to my expected output of:
<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-A*0/1/1*>Fa0/1/1</a>
<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-B*1/1*>Gi1/1</a>
<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-C*1/4*>Gi1/4</a>
<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-C*0/4*>Fa0/4</a>

Open in new window




Thanks very much.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sandeep KothariProject Lead

Commented:
try this...
   function PORT__renderCell(&$record){
	   $s = $record->val('SWITCH');
	   $port = $record->val('PORT');
	   $p = substr($port,2);
	   $ret = '<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*".$s."*".$p."* ">'.$port.'</a>';
	   return $ret;
   }

Open in new window

Sandeep KothariProject Lead

Commented:
igonre last one ... try this...

 function PORT__renderCell(&$record){
           $s = "Cisco-A";//$record->val('SWITCH');
           $port = "Fa0/1/1";//$record->val('PORT');
           $p = substr($port,2);
           $ret = "<a href='http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*".$s."*".$p."*' >".$port."</a>";
           return $ret;
   }

Open in new window

Author

Commented:
Hmm......might be a syntax thing.  It stops right at the first asterisk.

The href is only returning this  -->  
http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*

Open in new window



Thanks.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Sandeep KothariProject Lead

Commented:
try this ...
 function PORT__renderCell(&$record){
           $s = "Cisco-A";//$record->val('SWITCH');
           $port = "Fa0/1/1";//$record->val('PORT');
           $p = substr($port,2);
           $ret = "<a href='http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*".$s."*".$p."*' >".$port."</a>";
           return $ret;
   }

Open in new window

Sandeep KothariProject Lead

Commented:
 function PORT__renderCell(&$record){
           $s = $record->val('SWITCH');
           $port = $record->val('PORT');
           $p = substr($port,2);
           $ret = "<a href='http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*".$s."*".$p."*' >".$port."</a>";
           return $ret;
   }

Open in new window

Author

Commented:
Yes.......much better.....  one last thing...it seems that I missed an exception:

for IOS, they list the interfaces like this: Fa0/1/1, Gi1/1,  Fa0/4, etc.   But for CAT OS devices, they don't have prefixes,   only slot and port numbers like this 1/5, 9/4, 8/12

Can we modify your script above so that the $p variable will capture the whole entry if there is no Fa or Gi prefixing it?

thanks very much
Project Lead
Commented:
confirm if prefixes are present will they just be Fa and Gi ...


if yes. ... this will do... then remove this

$p = substr($port,2);

and use this...
$p = str_ireplace(array("Fa","Gi"),"",$port);

Open in new window


will work for prefixes and no prefixes ...
Most Valuable Expert 2011
Top Expert 2016
Commented:
It's not readily clear to me what you want in the URL, but whatever it might be, you almost certainly want to use this function.
http://www.php.net/manual/en/function.urlencode.php
Most Valuable Expert 2011
Top Expert 2016

Commented:
OK, I had a little time to look it over.  Here is how I would probably go about it.  I hope my assumptions about the data structure are helpful.
http://www.laprbass.com/RAY_temp_rley.php

The function is on line 30.  Created test data on line 55.  Proof of concept at line 80.  The %2F that you see is the HTML-encoded slash.

Best regards, ~Ray
<?php // RAY_temp_rley.php
error_reporting(E_ALL);


/* FROM THE POST AT EE
Row  SWITCH    PORT      output
1    Cisco-A   Fa0/1/1   *Cisco-A*0/1/1*
2    Cisco-B   Gi1/1     *Cisco-B*1/1*
3    Cisco-C   Gi1/4     *Cisco-C*1/4*
4    Cisco-C   Fa0/4     *Cisco-C*0/4*

<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-A*0/1/1*>Fa0/1/1</a>
<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-B*1/1*>Gi1/1</a>
<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-C*1/4*>Gi1/4</a>
<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=*Cisco-C*0/4*>Fa0/4</a>
*/

// THE PATTERN APPEARS TO BE
// ANCHOR TAG
// STAR
// SWITCH
// STAR
// SUBSTR(PORT,2)
// STAR
// END TAG
// PORT
// CLOSE TAG


function PORT__renderCell($record)
{
    // GET THE ARGUMENTS FROM THE OBJECT
    $sw = $record->val("SWITCH");
    $pn = $record->val("PORT");
    $p2 = substr($record->val("PORT"), 2);

    // HOOK UP THE BASE NAME WITH THE ARGUMENTS
    $url = '<a href="http://10.2.2.2/cgi-bin/vnresourceframe.cgi?dmn=0&grp=g-1&show=All&f_all_name=';
    $arg = '*'
    . urlencode($sw)
    . '*'
    . urlencode($p2)
    . '*'
    . '"'
    . '>'
    . $pn
    . '</a>'
    ;

    // RETURN THE LINK
    return $url . $arg;
}


// CREATE A TEST CLASS
Class TestData
{
    public function __construct($s, $p)
    {
        $this->s = $s;
        $this->p = $p;
    }

    public function val($n)
    {
        if ($n == 'SWITCH') return $this->s;
        if ($n == 'PORT')   return $this->p;
        return FALSE;
    }
}

// CREATE SOME TEST OBJECTS
$arr = array
( new testdata('Cisco-A', 'Fa0/1/1')
, new testdata('Cisco-B', 'Gi1/1')
, new testdata('Cisco-C', 'Gi1/4')
, new testdata('Cisco-C', 'Fa0/4')
)
;

// ITERATE OVER THE TEST OBJECTS
echo "<pre>";
foreach ($arr as $record)
{
    // SHOW THE OBJECT
    print_r($record);

    // RENDER AND SHOW THE HTML AS A LINK AND IN CLEAR TEXT
    $x = PORT__renderCell($record);
    echo  $x;
    echo PHP_EOL;
    echo htmlentities($x);

    // SPACING TO MAKE IT EASY TO READ
    echo PHP_EOL;
    echo PHP_EOL;
}

Open in new window

Sandeep KothariProject Lead

Commented:
Ray ... . I have seen your answers.. and I found that what you really do is most of the times use the answer already posted, create some test cases around that answer and repost it making it feel like its your response... but its a copied one.... I think that's how you reached to Genius position...  or else why always my answer is reposted again by you as a solution with ofcourse some extra test cases....
Most Valuable Expert 2011
Top Expert 2016

Commented:
@kshna:  Thank you for sharing.  

Please feel free to correct any of my code that I post here if you find it does not work correctly.  I am far from perfect and that is part of the reason that I lean toward posting tested examples.  If you feel that I am causing you trouble by posting answers with code examples here at EE, please use the "Request Attention" button to get a Moderator involved.  They are usually very helpful.

If you think that my comment and code snippet in response to this question is somehow the same as your comment and/or code snippets, I am sure you can make your case to a Moderator.

All the best, ~Ray
Sandeep KothariProject Lead

Commented:
what do you mean by I think its copied.... it is copied... have a look...

lines from my code posted much before than your code...
          $s = $record->val('SWITCH');
           $port = $record->val('PORT');
           $p = substr($port,2);

lines from your code...

    // GET THE ARGUMENTS FROM THE OBJECT
    $sw = $record->val("SWITCH");
    $pn = $record->val("PORT");
    $p2 = substr($record->val("PORT"), 2);

dont you think so ??

Author

Commented:
Hi Kshna,

   Thanks very much for this.  This one did the trick   --> $p = str_ireplace(array("Fa","Gi"),"",$port);

   Everything is working perfectly now.

Hi Ray,   Thanks for your post....in this particular case, there was no need for the uuencode function.



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial