1

I'm not too sure how I should word that title, apologies. x_x

I'm basically trying to convert a string to a formatted URL similar to how Reddit/Stackoverflow does it.

Eg. [Hello World](http://google.com) = Hello World

Both of the following work, but they don't work when combined together.

preg_replace("/\[([^\]]+)\]/", ... //Works for [Hello World]

preg_replace("/\(([^\)]+)\)/", ... //Works for (Hello World)

preg_replace("/\[([^\]]+)\]/\(([^\)]+)\)/", ... //Doesn't work

Regex confuses me x_x Help appreciated!

Sakuya
  • 660
  • 5
  • 23
  • Have you tried `/\[([^\]]+)\]\(([^\)]+)\)/`? Removing a `/` in the middle that would cause an error in the statement. – Jon Feb 21 '14 at 14:47
  • 2
    Its called markdown, Why not implement it properly, then you can use all the markdown to html markup features https://github.com/michelf/php-markdown or more parsers can be found here http://stackoverflow.com/questions/5116187/how-to-parse-markdown-in-php – Lawrence Cherone Feb 21 '14 at 14:50
  • and here http://stackoverflow.com/questions/605434/how-would-you-go-about-parsing-markdown – Lawrence Cherone Feb 21 '14 at 14:57

2 Answers2

0

Use this \[([^\[\]]*)\](.*)

$input_lines="[Hello World](http://google.com)";

preg_replace("/\[([^\[\]]*)\](.*)/", "$1", $input_lines);
Nambi
  • 11,944
  • 3
  • 37
  • 49
0
$str = '[Hello World](http://google.com)';
preg_replace('/\[([^\]]+)\]\(([^\)]+)\)/', '<a href="$2">$1</a>', $str);
vollie
  • 1,005
  • 1
  • 11
  • 20