Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/172995-checking-code-and-script-advice/
Share on other sites

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;

?>

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.

  • 3 weeks later...

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.

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.