Yanni Posted September 3, 2009 Share Posted September 3, 2009 Ill start by saying that im a complete nooblet when it comes to this and most of the code below is put together from various examples i have come across. What im trying to do is to have certain Names become automatic hyperlinks. So far what i have is from what i gather is a regular str_replace: <?php $data = array ( 'names' => array('name1', 'name2', 'name3'), 'links' => array('link1', 'link2', 'link3') ); $text = "[name1 and its friends name 2 and name3 should be replaced if this works]"; $numberoflinks = count($data['links']); for ($i=0;$i<=$numberoflinks;$i++){ $data['links'][$i] = "<a href=\"http://".$data['links'][$i]."\">".$data['names'][$i]."</a>"; } $text = str_replace($data['names'], $data['links'], $text); echo $text; ?> So far what i have tried to do is embed this in the template of a publishing page so that every time someone posts text with the given names they are automatically hyperlinked to the given pages. Problem is it doesnt really work and i have nowhere near enough php knowledge to figure out why. The other options from what i gathered was to have a mysql table containing the names and links and then have code that would query the table and replace the names with the links. The problem with this option is that being a complete noob im not sure how id go about doing it (would the could still be pasted in the template of the page?). Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/ Share on other sites More sharing options...
Mark Baker Posted September 3, 2009 Share Posted September 3, 2009 Well for ($i=0;$i<=$numberoflinks;$i++){ should be for ($i=0;$i<$numberoflinks;$i++){ Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/#findComment-911765 Share on other sites More sharing options...
Yanni Posted September 3, 2009 Author Share Posted September 3, 2009 Hmm its still not working. Everything from the quoted section on gets posted together with the text. Name1 and Name2 went for a jog in the park. array('name1', 'name2', 'name3'), 'links' => array('link1', 'link2', 'link3') ); $text = "[name1 and its friends name 2 and name3 should be replaced if this works]"; $numberoflinks = count($data['links']); for ($i=0;$i<$numberoflinks;$i++){ $data['links'][$i] = "<a href=\"http://".$data['links'][$i]."\">".$data['names'][$i]."</a>"; } $text = str_replace($data['names'], $data['links'], $text); echo $text; ?> Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/#findComment-911781 Share on other sites More sharing options...
Mark Baker Posted September 3, 2009 Share Posted September 3, 2009 The code works, but only if you have enabled PHP for your web server. If you're seeing the actual code, then your web server isn't running the PHP Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/#findComment-911797 Share on other sites More sharing options...
Yanni Posted September 3, 2009 Author Share Posted September 3, 2009 The site is built with Expression Engine which as far as i know is php and the hosting is done by enginehosting (EEs hosting branch). So i dont know why it would not be working. Ill inquire with EE and see what they say. EDIT: In the meantime, is there another way of doing this. I have thought of using a java script for greasemonkey but id like to find a more permanent sever side solution. Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/#findComment-911799 Share on other sites More sharing options...
mikesta707 Posted September 3, 2009 Share Posted September 3, 2009 your code works on my test server (Well excluding name2 but in your string its "name 2" gotta delete the space) Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/#findComment-911819 Share on other sites More sharing options...
Yanni Posted September 3, 2009 Author Share Posted September 3, 2009 Ok thanks for confirming that. Ill see what EE tells me. ` Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/#findComment-911834 Share on other sites More sharing options...
Yanni Posted September 18, 2009 Author Share Posted September 18, 2009 Sorry to bring this back but I solved my issue with having php scripts on the server but now that i can actually use it the script is not working the way i thought it would. I needed it to simply hyperlink "names". Instead in the current format it is creating a brand new line on the page with specific the name hyperlinked instead of hyperlinking all the existing ones. So the $text should be whatever the 'names' is instead of a brand new word. As usual any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/#findComment-920866 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.