如何将[link](#)替换为link?

时间:2022-12-09 21:07:45

I want to do something like *. actually changing this style []() to this style <a href=""></a>. here is my try:

我想做一些类似*的东西。实际上,将这个样式[]()更改为这个样式。这是我的尝试:

$str = '[link](#)';
$str = str_replace('[','<a href="',$str);     // output: <a href="link](#)
$str = str_replace(']','">',$str);            // output: <a href="link">(#)
$str = str_replace('(','',$str);              // output: <a href="link">#)
$str = str_replace(')','</a>',$str);          // output: <a href="link">#</a>

but now, I need to change link with #, how can I do that ?

但是现在,我需要改变与#的链接,我该怎么做呢?

1 个解决方案

#1


3  

You want to take a look at preg_replace(), with this you can use a regex to replace it, e.g.

您需要查看preg_replace(),您可以使用regex来替换它,例如。

$str = preg_replace("/\[(.*?)\]\((.*?)\)/", "<a href='$2'>$1</a>", $str);

regex explanation:

正则表达式的解释:

\[(.*?)\]\((.*?)\)
  • \[ matches the character [ literally
  • \[匹配字符[字面意思]
  • 1st Capturing group (.*?)
    • .*? matches any character (except newline)
      • Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
      • 量词:* ?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
    • . * ?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • 第一个抓捕小组*?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • \] matches the character ] literally
  • [\]匹配字符]字面意思
  • \( matches the character ( literally
  • \(匹配字符
  • 2nd Capturing group (.*?)
    • .*? matches any character (except newline)
      • Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
      • 量词:* ?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
    • . * ?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • 第二抓捕小组*?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • \) matches the character ) literally
  • \)匹配字符)字面上

#1


3  

You want to take a look at preg_replace(), with this you can use a regex to replace it, e.g.

您需要查看preg_replace(),您可以使用regex来替换它,例如。

$str = preg_replace("/\[(.*?)\]\((.*?)\)/", "<a href='$2'>$1</a>", $str);

regex explanation:

正则表达式的解释:

\[(.*?)\]\((.*?)\)
  • \[ matches the character [ literally
  • \[匹配字符[字面意思]
  • 1st Capturing group (.*?)
    • .*? matches any character (except newline)
      • Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
      • 量词:* ?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
    • . * ?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • 第一个抓捕小组*?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • \] matches the character ] literally
  • [\]匹配字符]字面意思
  • \( matches the character ( literally
  • \(匹配字符
  • 2nd Capturing group (.*?)
    • .*? matches any character (except newline)
      • Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
      • 量词:* ?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
    • . * ?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • 第二抓捕小组*?匹配任何字符(换行除外)量词:*?在零到无限的时间之间,尽可能少的时间,根据需要扩展(懒惰)
  • \) matches the character ) literally
  • \)匹配字符)字面上