Jump to content

MediaWiki format to HTML link


billli

Recommended Posts

Sorry to double post... :/

Hi!

 

I'm trying to convert two wiki like tags to their html counter part.

 

ie) [[WikiPage]] => <a href="index.php?p=wiki/view&page=WikiPage" rel="nofollow" title="WikiPage">WikiPage</a>

and

[[WikiPage|Title]] => <a href="index.php?p=wiki/view&page=WikiPage" rel="nofollow" title="Title">Title</a>

 

	
	$string = htmlentities($string);
	$simple_search = array(
		'/\[\[(.*?!\|)\|(.*?)\]\]/is',
		'/\[\[(.*?)\]\]/is');

	$simple_replace = array(	
		'<a href="index.php?p=wiki/view&page=$1" rel="nofollow" title="$2">$2</a>',
		'<a href="index.php?p=wiki/view&page=$1" rel="nofollow" title="$1">$1</a>'
	);


 

Could anyone help me with the two regular expressions?

 

Thanks

Link to comment
Share on other sites

I am not uber good at regexp but one way I see is this:

function match_wiki_tag($match)
{
$page = $match[1];
$title = ($match[3] && $match[2]) ?  $match[3] : $match[1];
return '<a href="index.php?p=wiki/view&page='.$page.'" rel="nofollow" title="'.$title.'">'.$title.'</a>';
}

preg_replace_callback('/\\[\\[([^|]+?)(\\|(.+?))?\\]\\]/i', 'match_wiki_tag', $str);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.