1

I am converting mediawiki markup to html. Stuck at converting image tag properly

Mediawiki Text with Image Tag

[[File:xyz2.png|none|thumb|748x748px]]

I m using preg_replace and following pattern works upto some extent

preg_replace(
"/\[\[(file):(.+?)\]\]/i",
"<img src='".explode("|",'$2',1)[0]."' alt=\"$6\"/>"
[[File:xyz2.png|none|thumb|748x748px]]
);

The output is

<img src="xyz2.png|none|thumb|748x748px" alt="">

However the needed output in this case is

<img src="xyz2.png" alt="">

What modifications in the match / replacement pattern can be done to achieve this ?

echoashu
  • 912
  • 3
  • 14
  • 31
  • `(.+?)` -> `([^|]*)` then you can just use `$2` in the replacement – Rizier123 Dec 30 '15 at 00:38
  • that outputs the comlete input string back – echoashu Dec 30 '15 at 00:43
  • You also need to add `.*` after it to match the rest, e.g. `/\[\[(file):([^|]*).*\]\]/i` – Rizier123 Dec 30 '15 at 00:47
  • awesome perfect ! please put it as an answer, I'll accept – echoashu Dec 30 '15 at 00:48
  • Trying to convert wikitext to html with a set of regular expressions is going to take a huge amount of effort to get even remotely right. You are far better off using one of the existing parsers. – Tgr Dec 30 '15 at 20:27
  • @Tgr agreed! tried Wiky php library, I'm using that to build up an mediawiki to direct wordpress import utility. Hence need to provide absolute image paths that goes into wordpress database. Open to suggestions!! thanks :-) – echoashu Jan 15 '16 at 02:57
  • 1
    The most reliable way is having MediaWiki do the work. Loading a MediaWiki page with `?action=render` will generate absolute URLs. – Tgr Jan 15 '16 at 19:33

1 Answers1

1

With your current regex you match everything until the two ending brackets with your second capturing group. But you can change it so you just grab everything until the first pipe and then you will have your filename, e.g.

preg_replace(
    "/\[\[(file):([^|]*).*\]\]/i",
    "<img src='$2' alt=\"$6\"/>",
    "[[File:xyz2.png|none|thumb|748x748px]]"
);
Rizier123
  • 58,877
  • 16
  • 101
  • 156