John Davidson

php - keep file links while using strip_tags function

0 comments
Message:


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


My Answer:
To keep the file links while using the strip_tags function, you can use a combination of strip_tags and preg_replace functions. Here's an updated version of your code that should achieve the desired result:

php
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(function($value) {
// Keep links while stripping other tags
$value = strip_tags($value, '');
// Extract the URL from the link
preg_match('/]*?\s+)?href=([\'"])(.*?)\1/', $value, $matches);
if (!empty($matches[2])) {
$value = $matches[2];
}
return $value;
}, $line);
fputcsv($fh, $data);
}
}
}

fclose($fh);


In this updated code, we use a custom function inside array_map to strip all tags except for the `
` tag. We then use a regular expression to extract the URL from the `` tag and replace the value with the URL if it exists. This way, the file links will be preserved in the output CSV file.

Rate this post

3 of 5 based on 7767 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us