Bendude14 Posted June 16, 2008 Share Posted June 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/ Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566204 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566212 Share on other sites More sharing options...
bluejay002 Posted June 16, 2008 Share Posted June 16, 2008 xml file... you can do all xpath's to that, convert to object or array, etc. pretty easy and very convenient. Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566214 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566217 Share on other sites More sharing options...
bluejay002 Posted June 16, 2008 Share Posted June 16, 2008 just create a valid xml file... then you might want to check out SimpleXML functions: http://www.php.net/manual/en/book.simplexml.php give it a ride... Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566222 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566233 Share on other sites More sharing options...
bluejay002 Posted June 16, 2008 Share Posted June 16, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566239 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566246 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 umm...do print_r($users); see if you really do have an array there... Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566250 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 this is the output Array ( [0] => Mike [1] => ryan [2] => ben ) Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566261 Share on other sites More sharing options...
sKunKbad Posted June 16, 2008 Share Posted June 16, 2008 Hey Bendude, Welcome to the phpfreaks forum. I read your post over in OWD, and glad things are working out. Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566288 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566334 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 can you post the full relevant code? maybe you made a clerical error somewhere.. Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566388 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566404 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566421 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566425 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566426 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 yes. depends on where you're getting the page from. Pulling it from a database? I assume no, from earlier. Is it in a flatfile? Pulling it from some other site? Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566429 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566430 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 file_get_contents() Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566432 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 Thanks that did the trick i stored the whole page in an include file then assigned the include file to the content variable using the file_get_contents() function. Solved thanks Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566454 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566462 Share on other sites More sharing options...
Bendude14 Posted June 16, 2008 Author Share Posted June 16, 2008 yes that is what im doing first of all i tried to just do it directly on the index.php page but because im echoing the new content to that page i get to copies of the page. Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566479 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 so...okay so like, are you saying that you put that parsing code inside index.php and so when you run index.php it is doing file_get_contents("index.php") on itself, and it is giving you two copies of the content? Quote Link to comment https://forums.phpfreaks.com/topic/110352-solved-dynamic-change-names-into-links/#findComment-566483 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.