Member Avatar for ___150

Hi,This code creates a friendly link on user mentions in comments...

However, it is not case sensitive and vice versa.

Examples;

@test works
@Test doesn't work

What do I do?

text = "@test @Test";

$pattern = "/\@(\w+)/"; preg_match_all($pattern,$text,$matches);
if($matches){  

$sql = "SELECT * FROM users WHERE username IN ('" .implode("','",$matches[1]). "')"; $user = $link->query($sql);

foreach($user as $i=>$u){
$text = preg_replace("/@".$u['username']."\b/",
"<a href='#' title='{$u['user_id']}'>@{$u['username']}</a> ", $text);
}
echo $text;
}

Recommended Answers

All 4 Replies

Assuming that your query SELECT * FROM users WHERE username IN ('test','Test') result is $user as:

0 => [
    'user_id'  => 1,
    'username' => 'test'
]
1 => [
    'user_id'  => 2,
    'username' => 'Test'
]

Output is:

<a href='#' title='1'>@test</a>  <a href='#' title='2'>@Test</a> 

Seems to me that it DOES work. What output were you expecting?

commented: A expressão regular pegar os dois valores. Mas a consulta não está pegando. Eu imaginei que seria uma collation case-sensitive no banco de dados. +0
Member Avatar for ___150

The regular expression takes both values. But the query is not catching. I figured it would be a case-sensitive collation on the database. But I am using utf8_general_ci (users) & utf8_unicode_ci (comments)...

But the query is not catching.

What do you expect the query to return?

Member Avatar for ___150

I want return the same @test is the same as @Test (regardless of letters)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.