We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
Website Script - Automatically change text to hyperlink?
MBNewB
Posts: 151 Forumite
in Techie Stuff
What I would like to do is write / download a script that checks for keywords on a page of text on my website and automatically rewrite them to hyperlinks.
For example I would have a keyword of MSE on my website multiple times in a paragraph of text.
The script would need to check the text for instances of MSE and automatically change them text to a link.
MSE = http://www.moneysavingexpert.com
leaving the actual text of the hyperlink intact
So it would look like MSE
I know I can do this manually for every occurance of the keywords but I would like to automate this to change text all over the site with ease.
PHP script would be ideal but I am willing to play with other languages as long as they will run ok on a PHP based website.
Thanks a lot!
For example I would have a keyword of MSE on my website multiple times in a paragraph of text.
The script would need to check the text for instances of MSE and automatically change them text to a link.
MSE = http://www.moneysavingexpert.com
leaving the actual text of the hyperlink intact
So it would look like MSE
I know I can do this manually for every occurance of the keywords but I would like to automate this to change text all over the site with ease.
PHP script would be ideal but I am willing to play with other languages as long as they will run ok on a PHP based website.
Thanks a lot!
0
Comments
-
If i'm understanding you correctly you want to alter your website content server side (e.g. before the user sees it)?
This could put a lot of unnecessary strain on the webserver when for the same effect you could just do a one off find/replace of the source code.
Edit: Some programs will allow you to do a find/replace over all open documents - simply open all of your webpagesEveryone needs a volume control -
When you shout every day and make everthing a catastrophe,
no one will hear you when you need to say something really important.0 -
Yes I would like something server side to fit in with the content management setup of the website.
I will be relying on people inputting text via their browser with possibly no knowledge of creating links etc...
Is it possible server side?
Thanks0 -
Yes - use the str_replace function of PHP.
http://uk2.php.net/manual/en/function.str-replace.php
This example should work well for you:
[PHP]// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);[/PHP]
Be very careful when dealing with user input, you should read up on SQL (and other types of) injection depending on what you're doing.
Everyone needs a volume control -
When you shout every day and make everthing a catastrophe,
no one will hear you when you need to say something really important.0 -
Thanks for that

I don't want to sound lazy but is there anyone who may be able to point me in the direction for the setup I need? Can't get my head around this using the type of example given?
Thanks0 -
Think this'll work but it's untested
[php]
$userinput = "This is an example of user input including keywords such as MSE, youtube and google";
$keywords = array("MSE", "youtube", "google");
$addresses = array("<a href=\"http://www.moneysavingexpert.com\">MSE</a>", "<a href=\"http://www.youtube.com\">youtube</a>", "<a href=\"http://www.google.com\">google</a>");
$newuserinput = str_replace($keywords, $addresses, $userinput); [/php]
i.e. you have an array of keywords you want to replace and an array of the websites you want these keywords to point to.
the str_replace function can be read as
"replace x with y in z"
i.e. replace keywords with addresses in userinputEveryone needs a volume control -
When you shout every day and make everthing a catastrophe,
no one will hear you when you need to say something really important.0 -
Thanks for the information you have posted already, I just have a question about this:
Will this work with just normal text that is built in the page. For example my site is ewagz.com, I to basically create a glossary of words for when these words are found they are automatically converted into hyperlinks for the variable i set to the words.
So will the above work for that, and will it work for numerous words (about 500-1000).
Any suggestions?0
This discussion has been closed.
Confirm your email address to Create Threads and Reply
Categories
- All Categories
- 352.5K Banking & Borrowing
- 253.7K Reduce Debt & Boost Income
- 454.5K Spending & Discounts
- 245.5K Work, Benefits & Business
- 601.4K Mortgages, Homes & Bills
- 177.6K Life & Family
- 259.4K Travel & Transport
- 1.5M Hobbies & Leisure
- 16K Discuss & Feedback
- 37.7K Read-Only Boards