keep file links while using strip_tags function

174 Views Asked by At

I'm using PHP to convert JSON data into an excel file without the HTML tags. everything works fine but some fields [ex:field_hcpg] include links and it was removed. below my try to remove all the HTML tags except links with no luck. please advise how to solve this issue.

below code move all HTML tags

fputcsv($fh,["title", "field_tags","field_risk_minimization_type","field_drug_class","field_hcpg","field_patient_card","field_risk_1","field_risk_minimization_type_1", "field_tags_1", "field_specialty_theraputic_area_","field_healthcare_provider_checkl","field_dhpc" ]);
if (is_array($jsonDecoded)) {
  foreach ($jsonDecoded as $line) {
    if (is_array($line)) {
  $data = array_map( 'strip_tags', $line);
  fputcsv($fh,$data);
}
  }
}
fclose($fh);

this script is my try to keep only the link

fputcsv($fh,["title", "field_tags","field_risk_minimization_type","field_drug_class","field_hcpg","field_patient_card","field_risk_1","field_risk_minimization_type_1", "field_tags_1", "field_specialty_theraputic_area_","field_healthcare_provider_checkl","field_dhpc" ]);
if (is_array($jsonDecoded)) {
  foreach ($jsonDecoded as $line) {
    if (is_array($line)) {
  $data = array_map( 'strip_tags', $line, ['<a>']);
  $data = preg_replace('~<a href="(https?://[^"]+)".*?>.*?</a>~', '$1', $data);
  $data = preg_replace('~<a href="(?!https?://)[^"]+">(.*?)</a>~', '$1', $data);
  fputcsv($fh,$data);
}
  }
}

The result of the last script is, which is wrong because I need to show the file url from the href tag

filename.pdf
1

There are 1 best solutions below

0
GUEST On

To strip tags from an array which your line appears to be, but leave anchors you can try

$data = array_map('strip_tags',$line,array_fill(0,count($line),'<a>'));

Example assumes your line is an array similar to this

array (
  0 => '<b><a href="#">text</a></b>',
  1 => '<b><a href="#">text</a></b>',
  2 => '<b><a href="#">text</a></b>',
)

Output

Array
(
  [0] => <a href="#">text</a>
  [1] => <a href="#">text</a>
  [2] => <a href="#">text</a>
)