php help

jimi_81

OSNN Senior Addict
Political Access
Joined
29 May 2002
Messages
820
simple question perhaps..
i was wondering how this input window creates hyperlinks just by the text typed... i imagine its a regular expression that parses the text..

is there an example snippet of this being done.
thanks
 
This helped me out a lot when I created a news submission system back in the past. Due to a lost backup (I know how you feel EP! :p) I can't give you an example.
 
ignoring bbtags, I would just whack anchor tags around strings begining with "www." or "http". But I'm simple what way
 
Khayman said:
ignoring bbtags, I would just whack anchor tags around strings begining with "www." or "http". But I'm simple what way
PHP:
<?php
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                     "<a href=\"\\0\">\\0</a>", $text);
?>
like that? me too :D
 
let me fix my server to serve up PHP source files again.

Done.
 
X-Istence said:
$text = preg_replace("!(^|\s)((.+)://(\w+[^\[\]\s/]+\.\w{2,})/?(\w+[^\[\]\s]+)?)!i", "<a href=\"\\2\" alt=\"\\4\">\\2</a> [\\4]", $url);
I am sure its stuff like that that just scares the bejeebus out of people :p
 
digging up an old thread here...(just over a month :) )

ok, just to be sure...

if i have a textarea, called $news.

say...
$news = "blah blah [bold]hey[/bold], and www.blah.com is blah blah blah";

do the reg ex's listed above parse $news and find each 'pattern'...the bold tag, and the link tags, etc.

and also, to make sure this would be done after selecting the data from the database.

any clarification, thanks
 
ok kill thread, got it figured out. thanks for the help guys!
 
Geffy said:
I am sure its stuff like that that just scares the bejeebus out of people :p

Sure as hell did when i first looked at regular expressions.

It is actually quite simple.

Code:
!(^|\s)((.+)://(\w+[^\[\]\s/]+\.\w{2,}\.?(:\w+[^\[\]\s/])?)/?(\w+[^\[\]\s]+)?)!i

Dissected below.

Code:
!(^|\s)

This tells it to match only if there is a space before it, or an \n. Otherwise, we don't do matching, this way text like "blahhttp://x-istence.com/" does not get matched.

The ! is the starting of the expression. It could also be a # symbol, it does not make a difference, as long as it is not one of the reserved ones: []\|(). Basically, use common sense. If you choose one you will rarely use in your pattern, then you won't have to do all the backward slashes to undo what you did. I use ! in this example.

Code:
(
A simple brace, it means everything inside this has to be available in the backwards slashes for us to use, for instance, the space above is available in \\1, so this will be \\2.

Code:
(.+)://
Match the <anythinghere>:// part. Straightforward, .+ means one or more characters.

This one is available (protocol only, not the ://) at \\3

Code:
(

Opening brace, this one will be available at \\4

Code:
\w+[^\[\]\s/]+\.\w{2,}\.?

This code is fairly simple. \w+ means to match one of any character at all, but we want to limit that, as we don't want spaces. So we add a bracket, and as the first symbol put a caret (^). This means that the rest should cause it to NOT match. So we don't want to match if there is a [, ], \s (any type of space). We add a + to the end of the stuff in brackets, this means it can match any number of characters (Or in this case, the opposite, not match those).

Next is the \. This is just a way to show that we want to match the literal period.

So far we have matched: www.somedomain

We want to match a . for com, net, org or any other, the \w{2,} means we want to match 2 or more characters at least, so that means we can also match hello.nl.

The last \.? is to match if there is an extra period in the domain, which is allowed to mean the literal domain, most browsers support this, so that URL's like this will work: http://osnn.net./index.php

Code:
(:\w+[^\[\]\s/])?
Look at the above for an explenation, all we are doing here is looking for the optional port number, so that matches like http://osnn.net:80/ will work flawlessly. The ? at the end means it can match 0 or more times. In most cases it will not be matched, and \\5 will be empty, specifying that no port is available.

Code:
)

Close \\4. This is used in our pattern to match just the hostname and port number, to make slashdot style links.

Code:
/?(\w+[^\[\]\s]+)?

We look for the optional stuff following the domain name, so that things like http://example.net/test/blah.html is also used when making it into a URL.

Code:
)!i

Close the entire thing, now \\2 is filled with goodiness, namely the entire URL without the extra space at the beginning which is stored in \\1.

The ! means it is the end of the pattern, the extra "i" is to make the pattern case insensitive. For more pattern modifiers check out http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php.

I hope this helps just a little bit :D.
 
someone glue that post somewhere - its going to be by regexp bible for the next millienia :D
 
btw, I recommend "Beginning Perl" by James Lee for really understanding Perl regular expressions
 
Sorry to bump, however, how would I use this on a string ?

For example I have
PHP:
$SomeVar = "text text text blah http://k-thx.com/mooses/ text text blah http://something.com blah blah";

Is there anyway to use the preg_replace you've written above to pick out and link the URI's in that string?
 
PHP:
$SomeVar = "text text text blah http://k-thx.com/mooses/ text text blah http://something.com blah blah";
$text = preg_replace( "`((http)+(s)?:(//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"http\\3://\\5\\6\\8\\9\" title=\"\\0\">\\5\\6</a>", $SomeVar);
try that?
 
Just a note. My pattern above did not work for things that contained multiple URL's in one line, or text for that matter, it would render it incorrectly. The correct version is as follows:

Code:
<?php

$pattern = "!((([^\[\]\s/]+)+)://([a-zA-Z\-\:\@\.]+\.[a-zA-Z]{2,}\.?((:\d+)?))/?\.?([a-zA-Z0-9-\+\?\!\@#\$%\^&\*\./=_:]+)?)!i";
$urlit = "<a href=\"\\1\" alt=\"\\4\" rel=\"nofollow\">\\1</a> [\\4]";

$url[] = "http://x-istence.com/";
$url[] = "http://x-istence.com/****it/something.php";
$url[] = "http://personal.x-istence.com/";
$url[] = "http://personal.x-istence.com/blog";
$url[] = "http://personal.x-istence.com";
$url[] = "irc://irc.osnn.net/osnn";
$url[] = "ftp://xistence:bloody@hello.com/";
$url[] = "javascript://alert(You are Hacked);.com/";
$url[] = "mailto://xistence@x-istence.com";
$url[] = "mailto:xistence@x-istence.com";
$url[] = "http://somet/microsoft.com/";
$url[] = "http://x-istence.com/some%20document%20withspaces.doc";
$url[] = "https://x-istence.com/some document withspaces.doc";
$url[] = "http://xistence.breached.x-istence.com:81/new.x-istence.com/";
$url[] = "http://xistence.breached.x-istence.com:81/new.x-istence.com/news.php?cwhat=comments&cid=7";
$url[] = "http://x-istence.com./index.php";
$url[] = "http://x-istence.com/.index.php";
$url[] = "https://blah.com/index";
$url[] = "http://mysite.defiant.osnn.net/";
$url[] = "Hello this is a bunch of text. With two URL's in it: http://x-istence.com/ and http://xistence.breached.x-istence.com";

foreach($url as $urlval)
    echo "<br>" .  preg_replace($pattern, $urlit, $urlval);

?>
 
Last edited:

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back