php help

jimi_81

OSNN Senior Addict
Political User
#1
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
 

Glaanieboy

OSNN Veteran Addict
#2
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.
 

Khayman

I'm sorry Hal...
Political User
#3
ignoring bbtags, I would just whack anchor tags around strings begining with "www." or "http". But I'm simple what way
 

SPeedY_B

I may actually be insane.
#4
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
 

X-Istence

*
Political User
#5

Geffy

OSNN Veteran Addict
#7
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
 

jimi_81

OSNN Senior Addict
Political User
#8
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
 

X-Istence

*
Political User
#10
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.
 

SPeedY_B

I may actually be insane.
#15
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?
 

Khayman

I'm sorry Hal...
Political User
#16
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?
 

X-Istence

*
Political User
#18
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,971
Messages
673,300
Members
89,016
Latest member
Poseeut