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); 
        }
    }
}
Sjon
  • 4,989
  • 6
  • 28
  • 46
Matus Mihely
  • 45
  • 2
  • 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 – rjdown Jul 12 '16 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?) – axiac Jul 12 '16 at 19:27
  • 1
    The problem isn't in the code you posted. – Charlotte Dunois Jul 12 '16 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? – Don't Panic Jul 12 '16 at 19:28
  • You could do this pretty easily with `preg_replace`. `@(\w+?)\b`. (depending on what valid characters are for a username) – chris85 Jul 12 '16 at 19:29
  • Thanks all. Yes I had that function in while loop so it was declared more than once. Can you write it as an answer so I can approve it for you? – Matus Mihely Jul 12 '16 at 19:45
  • 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. – rjdown Jul 12 '16 at 19:47

2 Answers2

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]);
Michal
  • 4,952
  • 8
  • 30
  • 63
  • Is it poossible with it add in front of every extracted word tag and behind it tag and put it back into the comment string with it? – Matus Mihely Jul 12 '16 at 20:21
  • Well this: `$updated_comment = preg_replace('/(@(\w+))/', '\1', $comment);` will return: `Hey @Name how are you?? And how is @AnotherName. Also have you seen @SomeName ??` Then you can just use php function `str_replace()`. I might find pure regex solution but that would take me a lot longer. – Michal Jul 12 '16 at 20:32
  • Also you will probably want specific href for link for every variable in sentence so it is better just to use `str_replace("@name", "$name", $updated_comment)`. Just use for loop on `$updated_comment` for every variable that you find in `$comment`. :) – Michal Jul 12 '16 at 20:34
  • Also someone in comments suggested using `sprintf()` that is probably much cleaner solution. – Michal Jul 12 '16 at 20:41
0

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

Matus Mihely
  • 45
  • 2
  • 7