This page is dedicated to discussions on the development of a better link formatter, i.e. a formatter supporting (optional) link titles, anchors, url parameters etc.

I start posting some code that I'm using to test regex for parsing forced links.
It should correctly parse links like the following

[[HomePage | (!Hello world;p=1&q=2) This is the home page with title]]

giving something like this: This is the home page with title.

The parentheses are used to select ;-separated URL options, like title or URL parameters. The space before the parenthesis is optional.
!Hello world corresponds to the title.
p=1&q=2 corresponds to the URL parameters.


//$pattern = "/^\[\[(\S*)(\s\(\!(.+)\))?(\s+(.+))?\]\]$/s";
//$pattern = "/\[\[(.*)\]\]/";
$pattern = "/\[\[([^\(\)\s]+)(\s*\((.+)\))*(\s+.+)*\]\]/";

if ($q) {
    print 'String: '.$q;   
    preg_match($pattern, $q, $matches);
    if (is_array($matches)) {
        $url = $matches[1];
        $desc = $matches[4];
        $optionlist = $matches[3];
        if ($optionlist) {
            $options = explode(";", $optionlist);  
            if (is_array($options)) {
                echo '<br />Options: ';
                foreach($options as $option){
                        case (preg_match("/!(.+)/", $option, $istitle)):
                        $title = $istitle[1];

                        case (preg_match("=", $option, $isparameter)):
                        $parameters = $isparameter[0];
            } else {
                echo '<br />Specified options are not valid';
        } else {
            print '<br />No options';
        echo '<br />Link chunks: ';
        echo '<br />Rendered link: '.$this->Link($url, '', $desc, '', '', $title).'<br />';
    } else {
        echo 'String does not match pattern';
} else {
    print 'Please enter a string';

There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki