Regex Php将括号和大括号转换为StackOverflow Reddit等链接 [英] Regex Php convert brackets and braces to links like StackOverflow Reddit

查看:94
本文介绍了Regex Php将括号和大括号转换为StackOverflow Reddit等链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将存储在mysql数据库中的文本框中的用户发布的一些标记解析为链接,类似于Reddit和StackOverflow的使用方式:

I'm trying to parse some markup posted by a user in a textbox stored in a mysql database into a link, similar to Reddit's and StackOverflow's style of using:

 [foo](http://foo.com) = <a href="http://foo.com">foo</a>

到目前为止,我已经想出了这个:

So far I've come up with this:

 if (stristr($text, '[') == TRUE && stristr($text, '](') == TRUE && 
     stristr($text, ')') == TRUE && strpos($text, '[') == 0) {
      $text = substr($text, 0, strpos($text, ']'));
      $href_start = strpos($text, '(');
      $href = substr($title, $href_start, strpos($text, ')'));
      $text_array = array('[', ']'); $href_array = array('(', ')');
      $href = str_replace($href_array, '', $href);
      $text_string = str_replace($text_array, '', $text_string);
      $text = '<a href="' . $href . '">' . $text_string . '</a>';
 }

有效,但仅在评论以链接开头时,而不是在链接出现在文本中间。我还需要从括号中的字符串中获取需要显示的文本标题的一定数量的文本,所以有时我会像这样编写最后一个字符串:

which works, but only when the comment starts with the link, and not when the link appears in a midst of text. I also need to grab a certain amount of text from the string that is in the brackets for text titles that need to be displayed, so sometimes I would write the last string like this:

$title = '<a href="' . $href . '">' . substr($text, 0, 80) . '</a>';

如果有人可以告诉我如何在执行此字符串操作时抓取可变数量的文本,那么奖励积分。我知道PHP Markdown,但我认为这对我正在尝试做的事情来说太过分了。

So bonus points if someone can tell me how to grab a variable amount of text while performing this string operation. I know about PHP Markdown, but I thought it would be overkill for what I'm trying to do.

推荐答案

$string = <<<EOS
some text [foo](http://foo.com) and
more text [bar](http://bar.com) and then
a [really looooooooooooooooooooooooooooooooooooooooooooooo00000000000000000ooong description](http://foobar.com)
EOS;


$string = preg_replace_callback(
  '~\[([^\]]+)\]\(([^)]+)\)~',
  function ($m) {
    return '<a href="'.$m[2].'">'.substr($m[1],0,80).'</a>';
  },
  $string
);

echo $string;

输出:

some text <a href="http://foo.com">foo</a> and
more text <a href="http://bar.com">bar</a> and then
a <a href="http://foobar.com">really looooooooooooooooooooooooooooooooooooooooooooooo00000000000000000ooong de</a>

编辑

我认为你不 使用 preg_replace_callback 为此 - 只需使用 preg_replace 相反,但我更喜欢前者,因为正则表达式更清洁,你可以更多地控制建立你的链接。但这里是 preg_replace 版本:

I suppose you don't need to use preg_replace_callback for this - can just use preg_replace instead, but I like the former better because the regex is a little cleaner and you can have a little more control over building your link. But here's the preg_replace version:

$string = preg_replace(
  '~\[([^\]]{1,80})[^\]]*\]\(([^)]+)\)~',
  '<a href="$2">$1</a>',
  $string
);

这篇关于Regex Php将括号和大括号转换为StackOverflow Reddit等链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆