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
To strip tags from an array which your line appears to be, but leave anchors you can try
Example assumes your line is an array similar to this
Output