Take the 2-minute tour ×

Within a function on my parent file, I am calling a function from an external php file. Here is my (simplified) code:

Parent file:

include "HelperFiles/htmlify.php";

function funcName(){
    $description = "some sample text"; 
    $description = htmlify($description, "code");

    echo $description;

htmlify.php file with called function:

$text = "";

function htmlify($text, $format){

    if (is_array($_POST)) {
          $html = ($_POST['text']);
        } else {
          $html = $text;

        $html = str_replace("‘", "'", $html); //Stripping out stubborn MSWord curly quotes
        $html = str_replace("’", "'", $html);
        $html = str_replace("”", '"', $html);
        $html = str_replace("“", '"', $html);
        $html = str_replace("–", "-", $html);
        $html = str_replace("…", "...", $html);

      if ($format == "code"){

        $html = str_replace(chr(149), "&bull;",$html);
        $html = str_replace(chr(150), "&mdash;",$html);
        $html = str_replace(chr(151), "&mdash;",$html);
        $html = str_replace(chr(153), "&trade;",$html);
        $html = str_replace(chr(169), "&copy;",$html);
        $html = str_replace(chr(174), "&reg;",$html);

       $trans = get_html_translation_table(HTML_ENTITIES);
       $html = strtr($html, $trans);

       $html = nl2br($html);
       $html = str_replace("<br />", "<br>",$html);

       $html = preg_replace ( "/(\s*<br>)/", "\n<br>", $html );  // seperate lines for each <br>
       //$text = str_replace ( "&amp;#", "&#", $text );
       //return htmlspecialchars(stripslashes($text), ENT_QUOTES, "UTF-8");

        return htmlspecialchars($html, ENT_QUOTES, "UTF-8");
      else if ($format == "clean"){
        return $html;


I'm getting the following error:

Notice: Undefined index: text in C:_Localhost_Tools\HelperFiles\htmlify.php on line 25

I've tried declaring the $text variable inside and outside of scope in multiple places but can not seem to get around this error (warning). Any help would be greatly appreciated! Thanks.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted


if (is_array($_POST)) {



and you should not get the warning anymore.

However I would recommend to remove this alltogether. The function parameter should always be used - everything else is confusing.

And you can also remove the first line in htmlify.php - that does basically nothing.

share|improve this answer
Sorry, should have mentioned that this function already existed and worked for a form using another parent file, which has a form in it. Even when I completely remove the if statement, it still has the same error –  John Nov 5 '12 at 20:44
Okay after removing it did switch to undefined variable $html which I then had to declare. so it did work. –  John Nov 5 '12 at 20:52
add comment

The error message reads undefined Index, not undefined variable. Look at all the places where you're trying to access an associative variable with text as key, $_POST['text'] seems to me to be your best bet, there's nothing that suggests that you're dealing with $_POST data AFAIK...

share|improve this answer
add comment

Your Answer


