Jump to content

Recommended Posts

I am building a website and all the members have there own profiles. I would like to link there name to the profile page every time there name is mentioned throughout the site. Can anyone suggest a good way to go about this without having to write out the links every time.  Also i do not have a database available for this site.

 

Thanks

Ben

Link to comment
https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/
Share on other sites

If you don't have a database, you're going to have to use a flatfile, or make a really long array of usernames and have that array included on all your pages.  You're basically going to take the content where a name may appear, and make a loop, looping through each name, and do some regex to search for that name and replace it with anchor tag wrapped around it. 

 

-make a list using a file/array (since no db)

-make a loop

-use regex to find and replace in the content. 

 

there's your battleplan.  If you get stuck on some code, post it and we'll try to help.

thanks for the reply. Ok i have created a flatfile and i can get the information out of the file into a variable using a loop. would it be better to explode it into a temp array? or is there another way to do it?

 

I have never used regular expressions so i will need some help there.

 

Thank You

ah ok i was using just a plain text file before so i would create an xml file like this?

 

<names>

<name>ben</name>

<name>james</name>

</names>

 

Then i need to loop through the xml file which will give me an array with the info in?

 

How do i go about replacing the names in the page with the link info?

 

str_replace

well, using a regular flatfile with names on separate lines vs. a xml file is more a matter of preference. To be honest, since all you're wanting to do is take a chunk of text and search through it for a name, xml is probably overkill.  I mean, you don't really need all the extra bells and whistles associated with using a xml file. 

 

Finding occurrences of the user's name in the chunk of text is the main trick here.  It really depends on what's in that chunk of text, vs. how accurate you want it to be.  Are you only wanting to link "valid" names? That is:

 

You would of course want this:

1. I know this girl named Jane who...

 

but, do you want it to link this?

2. I know this girl namedJanewho...

 

Well anyways, here is an example.  It does include making links like in #2...

 

// example array of names that you got from a flatfile
$users = array("Jane","John","Mike");
// example string 
$content = "I know this girl named Jane who likes this guy named John but John likes this guy Mike!";

// for each user in the array, make a new array with a linked version of the user
foreach($users as $name) {
   $linkednames[] = "<a href='path/to/$name'>$name</a>";
}

// in order to use preg_replace down there, you have to add / / around the name
// to specify the start and end of the search string.  so we're going to make an array 
// named pattern with the names like that. 
foreach($users as $name) {
   $pattern[] = "/$name/";
}

// preg_replace accepts arrays as arguments. Basically it does all the hard work for you
// internally it makes a loop to search through $content for each item in the $pattern array
// and when it finds it, it replaces it with the item in the same key position as $linkednames 
// array.  pretty nifty, huh? You can of course assign the results of preg_replace to a variable
// instead of echoing it. 
echo preg_replace($pattern,$linkednames, $content);

 

yeah... it may be overkill but no one said its a plain array right? if it is, then a flat text file would do... making a string with a delimiter then use explode is a nifty trick.

 

but its a complex one... and with searches that may a bit kinda complicated then xml would never be an overkill.

 

but hey, the options are laid, you have nice ideas here to begin... happy programming! :)

yer i agree that a text file should be ok for this. Here is the code i used to read the textfile and all seems to be working well

$textfile = 'data.txt';
if (file_exists($textfile) && is_readable($textfile)) {
// read the file into an array called $users
$users = file($textfile); }
// example string 
$content = "Here are a few names in my text file ben ryan and dave Mike!";

 

The problem is it only turns the last name in the text file into a link? any suggestions?

hey skunkbad yer this is a great forum with quick answers from the helpful members. I am still having the same problem where it only reads the last name in the textfile. I have made sure that there is no spaces at the ends of the line.

   

 <?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); }
// example string 
$content = "Here are a few names in my text file ben ryan and dave!";

// for each user in the array, make a new array with a linked version of the user
foreach($users as $name) {
   $linkednames[] = "<a href='path/to/$name'>$name</a>";
}

// in order to use preg_replace down there, you have to add / / around the name
// to specify the start and end of the search string.  so we're going to make an array 
// named pattern with the names like that. 
foreach($users as $name) {
   $pattern[] = "/$name/";
}

// preg_replace accepts arrays as arguments. Basically it does all the hard work for you
// internally it makes a loop to search through $content for each item in the $pattern array
// and when it finds it, it replaces it with the item in the same key position as $linkednames 
// array.  pretty nifty, huh? You can of course assign the results of preg_replace to a variable
// instead of echoing it. 
echo preg_replace($pattern, $linkednames, $content);
print_r($users);
?>

The Text file just simply has the names ben ryan dave each on a seperate line

ah okay I know what's going on.  Since it's a text file, each line ends with '\n' which signifies the end of for editors it needs to be trimmed off in order for the preg_replace to work:

 

<?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);
}


// example string 
$content = "Here are a few names in my text file ben ryan and dave!";

// for each user in the array, make a new array with a linked version of the user
foreach($users as $name) {
   $linkednames[] = "<a href='path/to/$name'>$name</a>";
}

// in order to use preg_replace down there, you have to add / / around the name
// to specify the start and end of the search string.  so we're going to make an array 
// named pattern with the names like that. 
foreach($users as $name) {
   $pattern[] = "/$name/";
}

// preg_replace accepts arrays as arguments. Basically it does all the hard work for you
// internally it makes a loop to search through $content for each item in the $pattern array
// and when it finds it, it replaces it with the item in the same key position as $linkednames 
// array.  pretty nifty, huh? You can of course assign the results of preg_replace to a variable
// instead of echoing it. 
echo preg_replace($pattern, $linkednames, $content);
print_r($users);
?>

By the way, I split up each foreach loop so you can better understand what's going on.  You can mash all of those loops together:

 

<?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); }
$content = "Here are a few names in my text file ben ryan and dave!";

// for each user in the array...
foreach($users as $key => $val) {
   // trim off '\n'
   $users[$key] = trim($val);
   // make linked version
   $linkednames[] = "<a href='path/to/{$users[$key]}'>{$users[$key]}</a>";
   // make pattern
   $pattern[] = "/{$users[$key]}/";
} // end foreach

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

 

Thanks that fixed the problem. Everything is working now the last bit i would like to try make easier is how i apply the whole page to the content variable. Is it possible to add the whole page to this one variable?

no the page is not been pulled from anyway. I have the page it just has a lot of names on and there will be more names added all the time this is why i wanted the php to automatically change the names into links to save time in the future. It would be possible for me to easily make the content of the page included using php.

I don't know how you got it setup but I got to ask: why not just use file_get_contents on page to begin with? I mean, can you explain why you need to store it in a different file before using file_get_contents()? Aren't you going to have to take the results and put it back into the original? In other words, it seems to me that you are doing this:

 

source -> copy to include file -> parse include file -> save as source.

 

Well, that's what it seems to me you just said.  So why not cut out the middle man?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.