Reputation: 5037
I need to convert the &
in a string to &
in php. I have a function to pull in some custom data in to an RSS feed. In this case it is a url which could look something like http://google.com?something=here&somethingelse=here&somethingelse=there
The problem is the amperstand shows in the feed and is not converted to &
so when I try using simplepie to fetch the feed it throws an error This XML document is invalid, likely due to invalid characters. XML error: not well-formed (invalid token)
This is the function I have to add the custom data to the feed:
/** Add ACF fields to RSS **/
add_action('rss2_item', 'cupusa_rss2_item');
function cupusa_rss2_item() {
global $post;
$memberEvent_link = get_field('member_event_link', $post->ID);
$output = '';
if( $post->post_type == 'jobs' ) {
$job_link_url = get_field('job_link', $post->ID);
$output .= "<job_link>{$job_link_url}</job_link>";
}
if($memberEvent_link){
$output .= "<member_event_link>{$memberEvent_link}</member_event_link>";
}
echo $output;
}
When using simplepie it is pulling this http://google.com?something=here&somethingelse=here&somethingelse=there
when it should be proper xml like this http://google.com?something=here&somethingelse=here&somethingelse=there
So my question is how do I modify my function above to convert the &
to &
for the $job_link_url
variable? Sometimes the link may have the & and sometimes it may not.
Upvotes: 0
Views: 254
Reputation: 944171
The htmlspecialchars
function will convert all characters which have special meaning in XML.
$job_link_url = get_field('job_link', $post->ID);
$xml_safe_job_link_url = htmlspecialchars($job_link_url, ENT_XML1 | ENT_QUOTES);
Upvotes: 2