I can't figure out how to do [[link|text]], I got regular [[link]] like this.

Pattern: /\[\[([^\n]+?)\]\]/i

Replacement text:
$text = $matches[1];

return "$text";

Can somebody help me please? Thanks.

Comments

Flying Drupalist’s picture

whoops, the replacement text is like this:


$text = $matches[1];

return "<a href=\"$base_path/wiki/$text\">$text</a>"; 
arhip’s picture

You can explode the matched text


$break = explode('|', $matches[1]);
$link = $break[0];
$text = $break[1];

return "<a href=\"$base_path/wiki/$link\">$text</a>";

or use new regex

/\[\[([^\n\|]+?)(\|([^\n]+?))?\]\]/i


return "<a href=\"$base_path/wiki/{$matches[1]}\">{$matches[3]}</a>";

Flying Drupalist’s picture

Hi and thanks arhip, but I don't think those work if you don't use the | pip. [[Link]] breaks.

ged3000’s picture

Do you mean that you want [[link]] to work as well as [[link|text]]?

Using the regex that arhip provided will detect [[link]] and [[link|text]], so you just need to make your return a bit cleverer:

if (strlen($matches[3]) > 0){
  return "<a href=\"$base_path/wiki/{$matches[1]}\">{$matches[3]}</a>";
}
else{
  return "<a href=\"$base_path/wiki/{$matches[1]}\">{$matches[1]}</a>";
}
Flying Drupalist’s picture

Status: Active » Fixed

Thanks a lot!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.