I’m trying to write this include file to include a function that passes a message and searchss for webURL’s (ie… www.kirupa.com) and replaces them (assuming the url is $word) with “<a href=$word target=_blank>$word</a>”
preciate it. it’s for a shoutbox that I’m playing with. I’ll post the finished *.inc file when i’m done. Maybe you’ll find some use for it, I’m trying to make it, what was the word… ****, for got it. Oh well, reusable. I hated programming concepts anyway :beam:
<?php
require 'processMessage.php';
$message = "some message from db or from get or post blah...";
$newMsg = processMessage($message);
echo $newMsg;
?>
processMessage.php
<?php
#--------------------------------------------------------------#
# PHP to parse URL's and email addresses before insertion into
# SQL database. Accepts a single string and returns a string.
#--------------------------------------------------------------#
//begin function to parse accepting a string
function processMessage($tempMessage)
{
if($tempMessage)
{
#--------------------------------------------------------------
# check for url's
#--------------------------------------------------------------
// check to see if the string contains the letters 'www'
if(stristr($tempMessage, "www"))
{
// create empty string to append to
$newMessage = "";
// if so, explode the string into an array at each space
$messageArray = explode(" ", $tempMessage);
// step through array
foreach($messageArray as $word)
{
//check to see if word contains www
if (stristr($word, "www") === false)
{
// if not, append it to the new string
$newMessage .= " " . $word;
}
else
{
// if so, strip the 'http://' off of it if included
$word = stristr($word, "www");
// format into a hyperlink (modify target frame)
$word = "<a href=http://$word target=_blank>$word</a>";
// append to new string
$newMessage .= " " . $word;
}
}
// set equal to another var
$emailMessage = $newMessage;
}
// if 'www' is not in the string...
if(!stristr($tempMessage, "www"))
{
// set to another var and move on
$emailMessage = $tempMessage;
}
#--------------------------------------------------------------
# Check for email's
#--------------------------------------------------------------
// check string for '@' and '.' to see if it includes an email address
if(stristr($emailMessage, "@") && stristr($emailMessage, "."))
{
// explode it if it does contain one
$messageArray = explode(" ", $emailMessage);
// create empty string to append to
$tempMessage = " ";
// step through array
foreach($messageArray as $word)
{
// if the word is not an email address, append and move on
if (stristr($word, "@") === false)
{
$tempMessage .= " " . $word;
}
// if it is, format it and append
else{
$word = "<a href=mailto:$word?subject=Waddup>$word</a>";
$tempMessage .= " " . $word;
}
}
}
// else if no email addy in string set to var
else
{
$tempMessage = $emailMessage;
}
// return the resultant string with URL's and emails formatted to hyperlinks
return $tempMessage;
}
}
?>
… Once again, with regex, you can do the above in a single statement, aside from the fact that the people at php.net reccomend using regex’s functions rather than str_replace
honeslty what you should do is search for a string with two “.” and then parse that as your link… just add an HT**TP:// if it isn’t already there… etc etc
yeah but if only a link is posted then that’s out the window. I’ll add a loop to account for the case no ‘www’ but ‘http’ and it’ll be fine but i have to also strip the punctuation off of the end of the link b/c looking at it, “www.kirupa.com,” won’t work b/c the link will be to “www.kirupa.com,”
uhm got a question what’s the difference between include() and require ?
Awligon, uhm can i use your script to check if the user places and email or a URL. I also made a shout box but it is a little buggy I want to make the name of the shoutee be the link to his/her email or website depending on whether he/she placed his/her email or url! I ended up placing both an email link and a url link. Also how can i not make it a link when the user didn’t or erronously enter something in the textfield.
And finally, i think loadVars took a little while longer to load data from the database compared to the loadVariables script I used in the Flash 5 version before. I don’t know maybe there’s a bug on my script?
//script i used to retrieve data...
view = new LoadVars();
view.retrieve = function(groupshout) {
this.sendAndLoad("shoutlist.php?shoutgroup="+groupshout, view, "POST");
this.onLoad = function(success) {
shouts.html = true;
if (success) {
totalshouts = parseInt(this.totalshouts);
amount.text = totalshouts;
if (totalshouts>0) {
shouts.htmlText = this.entries;
} else {
shouts.htmlText = "There are no entries in the shout box!";
}
sb.scrolling();
} else {
shouts.htmlText = "The data didn't load.";
}
};
};
view.retrieve(0);
//script i used to post shouts...
if (_parent.message.length<3 && _parent.name.length<3) {
_parent.message.text = "Required";
_parent.name.text = "Required";
} else if (_parent.name.length<3) {
_parent.name.text = "Required";
} else if (_parent.message.length<3) {
_parent.message.text = "Required";
} else {
_parent.note._visible = true;
shouted = new LoadVars();
shouted.name = _parent.name.text;
shouted.email = _parent.email.text;
shouted.site = _parent.site.text;
shouted.message = _parent.message.text;
shouted.sendAndLoad("shout.php", shouted, "POST");
shouted.onLoad = function(success) {
if (success) {
_parent._parent.gotoAndStop(1);
}
};
}