Fix My @ Tagging Class (Want it to work like Twitter) ($90)
$30-250 CAD
ถูกยกเลิก
โพสต์ ประมาณ 8 ปีที่ผ่านมา
$30-250 CAD
ชำระเงินเมื่อส่งงาน
I need a good php coder that knows what they're doing. I just lost $15 so decided it would take more. This has to be WORKING before I release funds. Please do not apply for the sake of applying. Read the job!
I have a function that is supposed to wrap a link around any username with an @ symbol in front of it. Right now it only partially works.
It will work on names in the DB if the known name comes first.
say JeremyBenson11 is in the DB
if the string is "Hello @JeremyBenson11 how are you."
It will wrap and <a></a> tag around it.
If the string is "There's @bob11 going with @JeremyBenson11"
It will not wrap the link around @JeremyBenson11
These are the known BUGS and Issues:
there are 2 issues:
- if the DB connection fails, you’ll get an "cannot call method prepare() on null" error
- preg_replace() does work, but you never save the modified string but return the original string.
issue #1:
1) never ever create DB connections inside a loop.
2) catch the exception where you can handle the problem. obviously that is not inside this method so the try..catch should be around that class (at most).
issue #3: you’ve already found str_replace().
note: the braces in [@]+ are superfluous. a character class of a single character equals that character.
note: by using @+(\w+) you get the username directly
note: foreach ($matches[1] as $name) ...
The function below look over it. PLEASE do not waste my time and money. If you suggest to put a variable in a prepared string as the solution I will NOT pay you.
---
public function handle_user_link($str){
if( preg_match_all('/([@]+\w+)/', $str, $matches)){
$matches = array_slice($matches, 0, 1);
for($i = 0; $i < count($matches); $i++)
{
// capture each tag
$tag = $matches[0][$i];
$name = ltrim($tag, '@');
try{
$db = new PDO($dsn, $dbUsername, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}catch(\PDOException $e){}
$sqlName = $db->prepare("SELECT `username` FROM `users` WHERE `username` = ?");
$sqlName->execute(array($name));
$retrievedUser = $sqlName->fetch();
if(!empty($retrievedUser))
{
// tagged user exists.
$str = str_replace($tag, '<a href="[login to view URL]'.$name.'">' . $tag . '</a>' , $str);
}
}
}
return $str;
// End handle_user_link
}