0

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); 
        }
    }
}
7
  • 3
    It appears that you are attempting to redeclare the userTags function. Maybe you are including the same file twice? Try require_once or include_once instead of require/include Commented Jul 12, 2016 at 19:26
  • The error message says it: you defined the function userTags() twice and this is not allowed. Maybe you defined it only once in a file that is included more than once in the same script? (Maybe through indirect inclusions?) Commented Jul 12, 2016 at 19:27
  • 1
    The problem isn't in the code you posted. Commented Jul 12, 2016 at 19:27
  • Can you show how you are calling it? You aren't accidentally using the function keyword again when you call it, are you? Commented Jul 12, 2016 at 19:28
  • 1
    I'd use sprintf instead. Much simpler and quicker! @MatusMihely nobody realised it was in a loop. You essentially answered it yourself, and it's encouraged that you create your own answer in this case. Commented Jul 12, 2016 at 19:47

2 Answers 2

1

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]);
Sign up to request clarification or add additional context in comments.

4 Comments

Is it poossible with it add in front of every extracted word <a> tag and behind it </a> tag and put it back into the comment string with it?
Well this: $updated_comment = preg_replace('/(@(\w+))/', '<a>\1</a>', $comment); will return: Hey <a>@Name</a> how are you?? And how is <a>@AnotherName</a>. Also have you seen <a>@SomeName</a> ?? Then you can just use php function str_replace(). I might find pure regex solution but that would take me a lot longer.
Also you will probably want specific href for link for every variable in sentence so it is better just to use str_replace("<a>@name</a>", "<a href='your/href'>$name</a>", $updated_comment). Just use for loop on $updated_comment for every variable that you find in $comment. :)
Also someone in comments suggested using sprintf() that is probably much cleaner solution.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.