...

View Full Version : Regex problem



The Reverend
01-23-2008, 08:01 AM
Okay, I'm creating a mod for some forum software and I'm trying to create a regex string that can search a post for all links and put them into an array.

Basically, what I have at this point is a variable that has the text in it and I need to search it.

Links can appear in four different formats


http://www.example.com
link (http://www.example.com)
link (http://www.example.com)
http://www.example.com


What I need to do is search a string for any of these formats and put them all into an array. I have next to no experience with regex and I was wondering if someone could help me out.

oesxyl
01-23-2008, 07:40 PM
Links can appear in four different formats


http://www.example.com
link (http://www.example.com)
link (http://www.example.com)
http://www.example.com



avoid first format because:
- I want to be text not link, what I must do?
- users are smart enought to know do find how to write links if they want
- is dificult to parse, what if is ftp://.. or others forms



$what = array("/\]+)\"?\]([^\[]+)\[\/url\]/i",
"/\[url\]([^\[]+)\[\/url\]/i");
$with = array("$1 $2", "$1");
$links = preg_replace($what, $with, $text);


is not tested, debug with print_r($variable) or echo/print.

for regex details, see:

[url]http://www.php.net/manual/en/ref.pcre.php (\"?([^\"\)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum