Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the word after a '@' character in PHP

Tags:

php

character

I'm making a news and comment system at the moment, but I'm stuck at one part for a while now. I want users to be able to refer to other players on the twitter style like @username. The script will look something like this: (not real PHP, just imagination scripting ;3)

$string = "I loved the article, @SantaClaus, thanks for writing!";
if($string contains @){ 
    $word = word after @;
    $check = is word in database? ...
}

And that for all the @username's in the string, perhaps done with a while(). I'm stuck, please help.

like image 207
user1930226 Avatar asked Dec 26 '12 16:12

user1930226


People also ask

How do I get the string after a specific character in PHP?

$data = "123_String"; $whatIWant = substr($data, strpos($data, "_") + 1); echo $whatIWant; Php Get String After Character.

How do I check if a string contains a specific word in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do I check if a string contains a specific character in PHP?

Using str_contains. The str_contains is a new function that was introduced in PHP 8. This method is used to check if a PHP string contains a substring. The function checks the string and returns a boolean true in case it exists and false otherwise.

How do I find a word in a sentence in PHP?

The first word of a sentence can be obtained with the help of various inbuilt functions like strtok(), strstr(), explode() and some other methods like using regular expressions.


2 Answers

This is where regular expressions come in.

<?php
    $string = "I loved the article, @SantaClaus! And I agree, @Jesus!";
    if (preg_match_all('/(?<!\w)@(\w+)/', $string, $matches))
    {
        $users = $matches[1];
        // $users should now contain array: ['SantaClaus', 'Jesus']
        foreach ($users as $user)
        {
            // check $user in database
        }
    }
?>
  1. The / at beginning and end are delimiters (don't worry about these for now).
  2. \w stands for a word character, which includes a-z, A-Z, 0-9, and _.
  3. The (?<!\w)@ is a bit advanced, but it's called a negative lookbehind assertion, and means, "An @ that does not follow a word character." This is so you don't include things like email addresses.
  4. The \w+ means, "One or more word characters." The + is known as a quantifier.
  5. The parentheses around \w+ capture the portion parenthesized, and appear in $matches.

regular-expressions.info seems to be a popular choice of tutorial, but there are plenty of others online.

like image 159
slackwing Avatar answered Oct 18 '22 03:10

slackwing


Looks like a job for preg_replace_callback():

$string = preg_replace_callback('/@([a-z0-9_]+)/', function ($matches) {
  if ($user = get_user_by_username(substr($matches[0], 1)))
    return '<a href="user.php?user_id='.$user['user_id'].'">'.$user['name'].'</a>';
  else
    return $matches[0];
}, $string);
like image 44
AndreKR Avatar answered Oct 18 '22 03:10

AndreKR