Matus Mihely
Matus Mihely

Reputation: 45

Recursive function in PHP return fatal error

I am trying to create function in PHP for looking for users like @Sometihng in string and then put link to their profiles into the string, but whenn I use it, it return Fatal error: Cannot redeclare userTags(). Do you know where is problem or what am I doing wrong? Here is my code. Thanks for any help.

$comment = "Hey @Name how are you?? And how is @AnotherName. Also have you seen @SomeName ??";

function userTags($startFrom) {

    $pos = strpos($comment, '@', $startFrom);     

    if ($pos !== false) {  

        $pos1 = strpos($comment, ' ', $pos);

        if ($pos1 !== false) {

            $insert_string = '<a href="profile.php?owner=somewhere">';
            $insert_string1 = "</a>";

            $comment = substr_replace($comment, $insert_string1, $pos1, 0);
            $comment = substr_replace($comment, $insert_string, $pos, 0);  

            userTags($pos1); 
        }
    }
}

Upvotes: 0

Views: 444

Answers (2)

Michal
Michal

Reputation: 5220

Try regex instead. :) '/(?<!\w)@\S+/' will extract all words starting with @character.

$comment = "Hey @Name how are you?? And how is @AnotherName. Also have you seen @SomeName ??";

preg_match_all('/(?<!\w)@\S+/', $comment, $matches);
print_r($matches[0]);

Upvotes: 1

Matus Mihely
Matus Mihely

Reputation: 45

Function was located in the while loop so it was declared more than once.

Upvotes: 0

Related Questions