简体   繁体   English

PHP preg_match URL格式

[英]PHP preg_match URL formatting

I'm not too sure how I should word that title, apologies. 我不太确定该如何称呼该标题,抱歉。 x_x X_X

I'm basically trying to convert a string to a formatted URL similar to how Reddit/Stackoverflow does it. 我基本上是想将字符串转换为类似于Reddit / Stackoverflow那样的格式化URL。

Eg. 例如。 [Hello World](http://google.com) = Hello World [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! 正则表达式使我感到困惑x_x帮助感激!

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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