Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with regular expression to convert links with ereg_replace

Status
Not open for further replies.

solice

Programmer
Feb 15, 2005
3
NL
Hello!

I've been strugling with this problem for a while, and every time I thought I figured it out, something else came up :(

This is my string which I want to replace with automatic URL's:

Code:
$originalMessage = "[URL unfurl="true"]http://www.thisonedoesntwork.com[/URL] [URL unfurl="true"]http://www.test.com[/URL] 
[URL unfurl="true"]http://www.moretests.com[/URL] [URL unfurl="true"]http://www.bla.com/hello.html[/URL]
[img]http://www.images.com/image01.jpg[/img]
";

And with this ereg_replace function I'm converting the url's in links:

Code:
$message = ereg_replace('(([^]]http+://[^<>[:space:]]+[[:alnum:]]))', " <a target=\"_blank\" href=\"\\2\">\\2</a>", $originalMessage);

Ofcourse I don't want the ??? to be altered, so I've added '[^]]' in the beginning of my ereg_replace function.

The good this is; it works... The bad thing; it doesn't alter the first URL in the $originalMessage string (
The reason why I'm so desperate is that before the " link, there's no "]" so it should work!

I hope you understand my question, thanks in advance for you answer(s) :)

Greetings, Maarten
 
i'm no expert on regex but i think you're looking at a word break issue. you will find if you add a space at the beginning of your string it works fine.
 
indeed! thanks for your answer... but I don't want to place a space in the beginning :(

when I place a space before " it works because the space isn't the "]" character...

but nothing; "", also isn't a "]" character... so I don't understand how I could build an extra check for the fact the URL is placed at the first position of my stringie...
 
from dim and distant memory there is something in the regex syntax that tells it to match only at word breaks. it may be that this is default behaviour and you need to add to the expression to reverse the effect.

or might it be that nothing is not any character and there is an alternative which means any null or alphanumeric except for "["?

there are some freeware regex syntax builders that you could google for? never tried any myself.

sorry not to be of more help.
 
OK, try this:

Code:
$message = ereg_replace('(^| )(http+://[^<>[:space:]]+[[:alnum:]])', " <a target=\"_blank\" href=\"\\2\">\\2</a>", $originalMessage);

Basically what it does is match either the start of the line or a space character. Will that work?

Personally, I would do the following. It's a little cleaner:

Code:
$message = preg_replace('`(^| )(http://[^ ]*\b)`', "$1<a target='_blank' href='$2'>$2</a>", $originalMessage);

The above code looks for the following:
Either the start of a line or a space: (^| )
followed by http://
followed by any number of non-space characters: [^ ]*
followed by a word boundary (i.e a space or end of line): \b
(the backticks mark the start and end of the regular expression)


You can't use a word boundary at the beginning because that will match the character ']' as well.

Hope that helps.
 
try this:

$message = preg_replace("/http:\/\/([^ ]+)/", "<a target='_blank' href=' $originalMessage);

ofcourse the will not be taken into account...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top