根据 url 值更改 bbcode

发布于 2024-12-02 05:20:23 字数 665 浏览 0 评论 0原文

我使用 preg_replace 和一个数组来遍历我的 bbcode 并在发布新闻条目时进行更改。

例如

$bbcode = array (
    "/\[url\=(.*?)\](.*?)\[\/url\]/is" => "<a href='$1' target='_blank'>$2</a>"
);

,当我从包含文本和 bbcode 的数据库(在本例中为 $newsPost)中提取数据时,我会执行此操作。

$newsPost  = preg_replace(array_keys($bbcode), array_values($bbcode), $newsPost);

现在,我想要查明 $1 的值是否包含我的域,目标应该是“top”,如果不包含,它应该是空白。

因此,如果我们有[url=http://www.mydomain.com]访问我们的页面[/url]。然后它会转换为 visit our page ,其他任何内容都会有 target =“_blank”

有什么想法吗?

提前致谢!

I'm using preg_replace with an array to go through my bbcode and make the change when a news entry is posted.

For example

$bbcode = array (
    "/\[url\=(.*?)\](.*?)\[\/url\]/is" => "<a href='$1' target='_blank'>$2</a>"
);

So then when I pull data from the database (in this example $newsPost) which contains the text and bbcode, I do this.

$newsPost  = preg_replace(array_keys($bbcode), array_values($bbcode), $newsPost);

Now, what I'd like is to find out if the value of $1 contains my domain, the target should be "top" and if not, it should be blank.

So if we have [url=http://www.mydomain.com]visit our page[/url]. then it's transformed to <a href="http://www.mydomain.com" target="top">visit our page</a> and anything else will have target="_blank".

Any ideas?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2024-12-09 05:20:23

编辑/固定

  • 添加了str_replace()来处理域名中的点
  • 最初我修复了你的反斜杠(如果你想在正则表达式中使用文字反斜杠,它应该是\ \ 在字符串中),但我已经撤消了这个,因为你已经说过它首先对你有用。

这个怎么样?

$mydomain = 'domain.tld';
$bbcode = array (
  "/\[url\=([^]]*)(".str_replace('.','\\.',$mydomain).")([^]]*)\]([^[]*)\[\/url\]/is" => "<a href='$1$2$3' target='top'>$4</a>",
  "/\[url\]([^[]*)(".str_replace('.','\\.',$mydomain).")([^[]*)\[\/url\]/is" => "<a href='$1$2$3' target='top'>$1$2$3</a>";
  "/\[url\=([^]]*)\]([^[]*)\[\/url]/is" => "<a href='$1' target='_blank'>$2</a>"
  "/\[url\]([^[]*)\[\/url\]/is" => "<a href='$1' target='_blank'>$1</a>",
);

如果这对您有用,请注意:不要将 $mydomain 设置为 www.domain.tld,而是将其设置为 domain.tld ,这样您就可以捕获所有子域。

您甚至可以使用多个域来完成此操作,如下所示:

$mydomains = array(
  'domain.tld',
  'anotherdomain.tld',
  'sub.yetanotherdomain.tld'
);

// Add domain-specific rules before general rules so we don't match domain
// specific links with the general link rule (we have replaced them by that point)
$domainrules = array();
foreach ($mydomains as $domain) {
  $domainrules["/\[url\=([^]]*)(".str_replace('.','\\.',$domain).")([^]]*)\]([^[]*)\[\/url\]/is"] = "<a href='$1$2$3' target='top'>$4</a>";
  $domainrules["/\[url\]([^[]*)(".str_replace('.','\\.',$domain).")([^[]*)\[\/url\]/is"] = "<a href='$1$2$3' target='top'>$1$2$3</a>";
}

// This array contains all your static BBCode rules
$staticrules = array(
  "/\[url\=([^]]*)\]([^[]*)\[\/url\]/is" => "<a href='$1' target='_blank'>$2</a>",
  "/\[url\]([^[]*)\[\/url\]/is" => "<a href='$1' target='_blank'>$1</a>",
  "/\[b\]([^[]*)\[\/b\]/is" => "<span class='bold_text'>$1</span>",
  ...
);

// Make an array that contains all the rules
$bbcode = array_merge($staticrules,$domainrules);

EDITED/FIXED

  • Added str_replace() to deal with dots in domain names
  • Initially I fixed your backslashes (if you want a literal backslash in your Regex it should be \\ in the string) but I have undone this as you have said it was working for you in the first place.

How about this?

$mydomain = 'domain.tld';
$bbcode = array (
  "/\[url\=([^]]*)(".str_replace('.','\\.',$mydomain).")([^]]*)\]([^[]*)\[\/url\]/is" => "<a href='$1$2$3' target='top'>$4</a>",
  "/\[url\]([^[]*)(".str_replace('.','\\.',$mydomain).")([^[]*)\[\/url\]/is" => "<a href='$1$2$3' target='top'>$1$2$3</a>";
  "/\[url\=([^]]*)\]([^[]*)\[\/url]/is" => "<a href='$1' target='_blank'>$2</a>"
  "/\[url\]([^[]*)\[\/url\]/is" => "<a href='$1' target='_blank'>$1</a>",
);

If that will work for you, a caveat: Don't set $mydomain to www.domain.tld, set it to domain.tld, so you catch all subdomains.

You could even do it with multiple domains like this:

$mydomains = array(
  'domain.tld',
  'anotherdomain.tld',
  'sub.yetanotherdomain.tld'
);

// Add domain-specific rules before general rules so we don't match domain
// specific links with the general link rule (we have replaced them by that point)
$domainrules = array();
foreach ($mydomains as $domain) {
  $domainrules["/\[url\=([^]]*)(".str_replace('.','\\.',$domain).")([^]]*)\]([^[]*)\[\/url\]/is"] = "<a href='$1$2$3' target='top'>$4</a>";
  $domainrules["/\[url\]([^[]*)(".str_replace('.','\\.',$domain).")([^[]*)\[\/url\]/is"] = "<a href='$1$2$3' target='top'>$1$2$3</a>";
}

// This array contains all your static BBCode rules
$staticrules = array(
  "/\[url\=([^]]*)\]([^[]*)\[\/url\]/is" => "<a href='$1' target='_blank'>$2</a>",
  "/\[url\]([^[]*)\[\/url\]/is" => "<a href='$1' target='_blank'>$1</a>",
  "/\[b\]([^[]*)\[\/b\]/is" => "<span class='bold_text'>$1</span>",
  ...
);

// Make an array that contains all the rules
$bbcode = array_merge($staticrules,$domainrules);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文