0

I have the following function:

function MyReplaceListTag($myText,$number){ 

    if (strpos($myText,'<li>')===FALSE){

        $myResult= strip_tags($myText);
        return $myResult;

    }else{
        $number++;
        $pattern= '/<li>/';
        $replacement = "\n".$number."- ";
        $myText=preg_replace (  $pattern ,  $replacement ,  $myText , 1 );

        MyReplaceListTag($myText,$number);


    }
}

I call it with:

$result = MyReplaceListTag( $testTEXT,0);

Nothing is returned, var_dump($result) gives NULL.

I must be doing something incredibly stupid, but what?

2
  • 3
    If you pass a value in $myText that contains <li> then the function doesn't return anything (you don't have a return statement in the else block or at the bottom), which PHP treats as NULL return Commented May 2, 2016 at 11:47
  • show the sample text Commented May 2, 2016 at 11:47

1 Answer 1

1

Your function is returning NULL on the else block because it's lacking a return statement.

Change the else block from:

MyReplaceListTag($myText,$number);

to

return MyReplaceListTag($myText,$number);

Ideone Demo

Sign up to request clarification or add additional context in comments.

1 Comment

You're very welcome @mvdn. If my answer helped you, please mark it as the correct answer, thank you !

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.