简体   繁体   中英

PHP preg_match URL formatting

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!

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

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

preg_replace("/\[([^\[\]]*)\](.*)/", "$1", $input_lines);
$str = '[Hello World](http://google.com)';
preg_replace('/\[([^\]]+)\]\(([^\)]+)\)/', '<a href="$2">$1</a>', $str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM