phpreplacehyperlinkphpexcel

PHPExcel str_replace


i want to convert my excel hyperlink's path to open links with PHPExcel.

$links = $objPHPExcel->getActiveSheet()->getHyperlinkCollection();

This method will return an array of hyperlink objects, indexed by cell address; and you could then use array_filter() with an appropriate callback and the ARRAY_FILTER_USE_KEY flag set to extract those within a specific range.

my var_dump($links); output :

enter image description here

But i dont know how to loop the array of objects with array_filter() function..

I try :

 $test = str_replace($links, "file", "mnt");
 var_dump($test);

and i got the error above.. Any ideas please ?


Solution

  • The collection is an array of objects, indexed by cell address; and the PHPExcel_Cell_Hyperlink object has a set of documented methods for accessing and setting its data:

    foreach($links as $cellAddress => $link) {
        // get the URL from the PHPExcel_Cell_Hyperlink object
        $url = $link->getUrl();
        // change the URL however you want here
        $url = str_replace($url, "file", "mnt");
        // Set the new value for the link
        $link->setUrl($url);
    }
    

    If you want to modify just those URLs for cells in column N, then you can wrap them in an if test:

    foreach($links as $cellAddress => $link) {
        // Test for column N
        sscanf($cellAddress, '%[A-Z]%d', $column, $row);
        if ($column == 'N') {
            // get the URL from the PHPExcel_Cell_Hyperlink object
            $url = $link->getUrl();
            // change the URL however you want here
            $url = str_replace($url, "file", "mnt");
            // Set the new value for the link
            $link->setUrl($url);
        }
    }