TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-11-2008, 11:44 PM   #1 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default Find HTTP:// and create link

Hello all.

I would like to search a DIV and find the static, nonlinked,
http://www.website.com inside the DIV and make it an active. Is there a simple way to do this? I know I can search the div somehow, but how do I replace?

Thanks!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 12-12-2008, 02:48 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Try the following code:

javascript Code:
function TalkPHP_MakeLinks(pWithinElement)
{
    if (!pWithinElement)
    {
        return false;
    }
   
    var szContent = pWithinElement.innerHTML;
    pWithinElement.innerHTML = szContent.replace(/(http:\/\/.+?)[\s|\n]/ig, '<a href="$1">$1</a> ');
}
Attached Files
File Type: html TalkPHP_MakeLinks.html (608 Bytes, 149 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-12-2008, 02:58 PM   #3 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Big Grin

Quote:
Originally Posted by Wildhoney View Post
Try the following code:

javascript Code:
function TalkPHP_MakeLinks(pWithinElement)
{
    if (!pWithinElement)
    {
        return false;
    }
   
    var szContent = pWithinElement.innerHTML;
    pWithinElement.innerHTML = szContent.replace(/(http:\/\/.+?)[\s|\n]/ig, '<a href="$1">$1</a> ');
}
Thanks for the response, works perfect. I am not sure I understand how it is working however. Are these comments correct:

function TalkPHP_MakeLinks(pWithinElement)
{
//if there is no DIV with this name, do nothing.
if (!pWithinElement)
{
return false;
}

//this var equals the DIV to be "edited"
var szContent = pWithinElement.innerHTML;
//this finds the HTTP and makes it a link...
pWithinElement.innerHTML = szContent.replace(/(http:\/\/.+?)[\s|\n]/ig, '<a href="$1">$1</a> ');
}

Can you explain this:

http:\/\/.+?)[\s|\n]/ig


Thanks again!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 12-13-2008, 12:17 AM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

/(http:\/\/.+?)[\s|\n]/ig creates a regular expression object with the pattern (http:\/\/.+?)[\s|\n] and flags i (case insensitive) and g (global).

The pattern itself searches for (and retains for later) the characters "http://" followed by one or more (+) single characters (.; 'single character' means any character except a newline). The question mark (?) makes the 'one or more single characters' what is called ungreedy (which is important but we can gloss over it right now). Next, [\s|\n] matches either a whitespace character (tab, space, newline), a vertical pipe character or a newline character (I think Wildhoney really meant just \s so that the match stops at whitespace (thanks to the ungreedy repetition earlier)).

Put simply, we match all instances of "http://" followed by one or more characters, followed by a whitespace character.
Salathe is offline  
Reply With Quote
Old 12-13-2008, 12:47 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Thanks Salathe. Yes, I did mean that. Either a space or a line break, but it would seem a space in this context behaves as a new line as well. Why is this? I thought it would look for a space, and so I had to specify either a space or a new line.

The following regular expression appears to work just fine, even if the link appears at the end of a line followed by a line break:

Code:
/(http:\/\/.+?)\s/ig
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-13-2008, 01:06 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The \s escape sequence matches any whitespace character which includes the space character along with such beauties as the tab and newline characters.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Create your own advanced WYSIWYG editor almsamim Javascript, AJAX, E4X 8 10-23-2008 06:41 PM
hiding original link problem webtuto Absolute Beginners 2 10-17-2008 05:56 PM
Need your feedback Tanax Absolute Beginners 29 10-11-2007 04:50 PM
Trying to create a "sign up form"....help please RogueDogg Absolute Beginners 31 06-03-2005 06:26 AM


All times are GMT. The time now is 08:43 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design