PDA

View Full Version : search for and parse hyperlinks



awligon
August 30th, 2003, 12:50 AM
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>"



<?php
function processMessage($message) {
if (!strpos($message, "www")){
string($newMessage = "");
$messageArray = explode(" ", $message);
foreach($messageArray as $word){
if (!strpos($message, "www")){
$word = '<a href=$word target=_blank>$word</a>';
$newmessage .= " " .$word;
}
else{
$newmessage .= " " .$word;
}
}
$message = $newMessage;
}
return $message;
}
?>

awligon
August 30th, 2003, 02:46 AM
got it. For once Ahmed didn't have to bail me. :}


<?php
function processMessage($tempMessage)
{
$newMessage = "";
$messageArray = explode(" ", $tempMessage);
print substr($message, $needle);
foreach($messageArray as $word){
if (stristr($word, "www") === false){
$newMessage .= " " . $word;
}
else {
$word = "<a href=$word target=_blank>$word</a>";
$newMessage .= " " . $word;
}
}
return $newMessage;
}
?>

ahmed
August 30th, 2003, 02:55 AM
haha.. i missed this thread, sorry :)

I like how you've done it though, if it was for me I might've ended up using regex, which make it unneccesarily complicated

awligon
August 30th, 2003, 03:46 AM
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:

ahmed
August 30th, 2003, 03:50 AM
lol :P

To be honest I do need a link parser for a lightweight blog software i wrote a few weeks ago.. I was lazy too lazy to make my own parser :P

awligon
August 30th, 2003, 06:16 AM
someFile.php


<?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;
}
}
?>

ahmed
August 30th, 2003, 10:15 AM
good job!

Here's my crappy version:
<?php

function parseURL( $message )
{
$message2 = str_replace ( "" , ">" , $message2 );
echo $message2;
}

$mymessage = "kirupa ( , "<a href=" , $message );
$message2 = str_replace ( "" , "</a>" , $message2 );
$message2 = str_replace ( )";

parseURL( $mymessage );

?>.. 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 :)

awligon
August 30th, 2003, 01:40 PM
found an error but I added an else statement at the end to take care of case: url=true && email=false. Works now :thumb:

eyezberg
August 30th, 2003, 03:44 PM
what about a url like my Ooooold site http://eyezberg.free.fr ?
no www in here...

Digitalosophy
August 30th, 2003, 09:23 PM
AW that's pretty cool, nice job

awligon
August 31st, 2003, 02:03 PM
Good call eyezberg, although I am highly annoyed now, it's a good point. ;) I'll fix it up when I get a chance.

Jubba
August 31st, 2003, 02:24 PM
honeslty what you should do is search for a string with two "." and then parse that as your link... just add an HTTP:// if it isn't already there... etc etc

awligon
August 31st, 2003, 10:11 PM
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,"

Thanks though Jubba.

comicGeek
August 31st, 2003, 11:22 PM
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.

Uhm i got a working sample here --> www.keldesigned.com/test

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);
}
};
}

awligon
September 1st, 2003, 11:46 AM
wow. umm... feel free to use the script, but I have no clue what you are asking for or telling or what. Please clarify.