Replace placeholder in XML file with PHP function

I have an XML file that contains FAQs, but some of the contents of the answers need to use PHP functions to output appropriate content.

How can I find the placeholders within the answers, and replace them with PHP functions? Ideally, I would like to be able to have the functions set as variables to be changeable across multiple websites this code would be used on.

XML File (placeholders in last block, %LOCAL_NO% and %PREM_NO%)

<?xml version="1.0" encoding="UTF-8"?>
<faqs>
    <faq>
        <category>General</category>
        <q>How old do I have to be to use your service?</q>
        <a>You must be at least 18 years of age.</a>
    </faq>
    <faq>
        <category>General</category>
        <q>How long is a psychic reading?</q>
        <a>The length of a psychic reading is completely up to you. It depends on the number and complexity of the questions you ask. The average length of a reading is 15 to 20 minutes.</a>
    </faq>
    <faq>
        <category>General</category>
        <q>Can I choose the psychic I speak with?</q>
        <a>Of course! You can choose who you would like to speak to by selecting your desired psychic's profile and following the online prompts via the online booking page, or call us on %PREM_NO% and enter their PIN, or call %LOCAL_NO% and our live receptionists will connect you to a psychic that matches your requirements!</a>
    </faq>
</faqs>

PHP output

<?php // General FAQs
$faqGeneral = $xml->xpath("/faqs/faq[category='General']");
echo "<h2>General</h2>";
foreach ($faqGeneral as $faq) { ?>
    <h3><?php echo $faq->q; ?></h3>
    <p><?php echo $faq->a; ?></p>
<?php } ?>


Source: xml

Leave a Reply