Jump to content

Every time name appears create link


Bendude14

Recommended Posts

Ok what i want to do is every time a name appears and it exists in the profiles tables i want it to become a link to that persons profile.  could some one give me any idea's on how to go about this?

 

I was doing it using a text file like below but i now have a database and figure it would be better to do it that way

 

    <?php
// names that you got from a flatfile
$textfile = 'data.txt';
if (file_exists($textfile) && is_readable($textfile)) {
// read the file into an array called $users
$users = file($textfile); }
// trim '\n' from each user
foreach($users as $key => $val) {
   $users[$key] = trim($val);
}


$content = file_get_contents("inc/index.inc.php", $incpath = false, $resource_context = null);
// for each user in the array, make a new array with a linked version of the user
foreach($users as $name) {
   $linkednames[] = "<a href='http://address/#$name'>$name</a>";
}


foreach($users as $name) {
   $pattern[] = "/$name/";
}

echo preg_replace($pattern, $linkednames, $content);

?>

 

 

Thanks

Ben

Link to comment
https://forums.phpfreaks.com/topic/119611-every-time-name-appears-create-link/
Share on other sites

$query = "select * from users";
$result = mysql_query($query, $db_connection);
$content = file_get_contents("inc/index.inc.php", false, null);
while ($row = mysql_fetch_assoc($result)) {
    $content = str_replace($result['name'], '<a href="#">'.$result['name'].'</a>', $content);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.