Jump to content

[[title|url]]


neoform

Recommended Posts

I want to split this, but for some reason if i input this string:

 

$str = '[[Cool Link|http://www.blah.com/]] [[hi there]]';
$str = preg_replace("`\[\[(.*)\|(.*)\]\]`siU", "<a href=\"\\2\">\\1</a>", $str);

 

I get a messed up link that grabbed more than it should (I thought putting a U at the end makes it ungreedy..

Link to comment
Share on other sites

I apparently oversimplified my problem..

 

	$str = "[[url With Other Stuff in It|Link Title]] [[internal_link]] [[image: image_name.jpg|[[image Title and Link]]]]";

echo $str."<br />";

$str = preg_replace("`\[\[(?!Image:)(.*)\|(.*)\]\]`siU", "<a href=\"\\1\">\\2</a>", $str);
$str = preg_replace("`\[\[(?!Image:)(.*)\]\]`siU", "<a href=\"\\1\">\\1</a>", $str);
$str = preg_replace("`\[\[image:(.*)\]\]`siU", " ### \\1 @@@ ", $str); //to be completed

echo $str;

Link to comment
Share on other sites

The pattern is ungreedy (lazy), but the engine always tries to make a match. .* does not indicate any stopping points, so the engine overshoots ]] to look for a pipe. Make this more specific, e.g.: `\[\[(?!Image:)([^\[\]]*)\|(.*)\]\]`siU

 

P.S. Don't you want to use + instead of *?

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.