1

I have a PHP function that is calling the following:

function availability_filter_func($availability) {
    $replacement =  'Out of Stock - Contact Us';
    if(is_single(array(3186,3518)))
        $availability['availability'] = str_ireplace('Out of stock', $replacement, $availability['availability']);
}

As you can see I am replacing the text string with custom text, however I need the "Contact Us" text to be <a href="mailto:[email protected]">Contact Us </a> - how would I go about doing this? Inputting raw html into the replacement string makes the html a part of the output.

Echo does not work and breaks the PHP function - same with trying to escape the PHP function, inserting html on on the entire string, and unescape the function.

Any advice would be appreciated, thank you.

4
  • Why is it bad practice to omit curly braces Commented Apr 29, 2015 at 21:10
  • If you're seeing the HTML in the output, the problem is with how you're outputting it. You must be calling htmlentities() or htmlspecialchars() on the string, which encodes all the HTML markup. If you want to be able to put HTML into the string, you mustn't do that. Commented Apr 29, 2015 at 21:12
  • Changing str_ireplace("Out of stock", htmlentities("<p style="color:red">This text is red</p>") results in the PHP script breaking. I have also tried str_ireplace("Out of stock", htmlentities('<p style="color:red">This text is red</p>') Commented Apr 29, 2015 at 21:19
  • In both your examples ^ you're not closing the str_ireplace function, you're only close the htmlentities. Also why are you running the htmlentitites, don't you want the HTML outputted? Commented Apr 29, 2015 at 21:38

2 Answers 2

2

Your code seems to be a complex way of doing this

function availability_filter_func($availability) {
    $default = "Out of Stock";
    $string = "<a href='mailto:[email protected]'>Contact Us</a>";
    if($availablity !== "") {
        $return = $availability;
    } else {
        $return = $default;
    }
    return "$return - $string";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try the Heredoc Syntax

$replacement = <<<LINK 
<a href="mailto:[email protected]">Contact Us</a>
LINK;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.