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:contact@example.com">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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Bluetower
  • 33
  • 1
  • 4
  • [Why is it bad practice to omit curly braces](http://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1) – Barmar Apr 29 '15 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. – Barmar Apr 29 '15 at 21:12
  • Changing `str_ireplace("Out of stock", htmlentities("

    This text is red

    ")` results in the PHP script breaking. I have also tried `str_ireplace("Out of stock", htmlentities('

    This text is red

    ')`
    – Bluetower Apr 29 '15 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? – chris85 Apr 29 '15 at 21:38

2 Answers2

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:contact@example.com'>Contact Us</a>";
    if($availablity !== "") {
        $return = $availability;
    } else {
        $return = $default;
    }
    return "$return - $string";
}
0

Try the Heredoc Syntax

$replacement = <<<LINK 
<a href="mailto:contact@example.com">Contact Us</a>
LINK;
Aviz
  • 281
  • 2
  • 9