简体   繁体   English

我正在制作一个图像板,无法弄清楚链接引用

[英]I'm making an imageboard and can't figure out link quoting

What I'm trying to do is fairly simple, but I can't figure it out. 我想做的事情相当简单,但我无法弄清楚。 I'd like for a body of text to be scanned for anything that looks like >>1234 and be replaced with <a href="1234">>>1234</a> I can't conceive of a way to do this, although I can do parts. 我希望扫描<a href="1234">>>1234</a>文本以查找>>1234并用<a href="1234">>>1234</a>替换的任何内容我无法想到这样做的方法虽然我可以做部分。 Writing a regex is easy enough for finding instances of >>1234 but it can't help me with the rest. 编写正则表达式很容易找到>>1234实例,但它对其余部分无法帮助我。 This realistically can be done in two ways, the client being fed the text as-is and then using javascript to format it, or the server processing the text each time and spitting back out the formatted version. 这实际上可以通过两种方式完成,客户端按原样提供文本,然后使用javascript格式化,或者服务器每次处理文本并吐出格式化版本。

Here's the github page: https://github.com/4tran/VIB 这是github页面: https//github.com/4tran/VIB

Here's an example of a regex I've tried that has worked somewhat: 这是一个我尝试过的正则表达式的例子:

<?php
function formatLink($post) {
    if(preg_match('/^\>\>([0-9]+)$/', $post)) {
      echo $post;
    }
}
formatLink(">>10"); // returns ">>10"
formatLink("spaghetti"); // returns ""
formatLink(">>10 spaghetti"); // returns ""
?>

Any help would be greatly appreciated, I'm really stuck here. 任何帮助将不胜感激,我真的被困在这里。

EDIT: I FIGURED OUT SOMETHING. 编辑:我想出了一些东西。 Okay, so for anyone who'd like to help, I now have a very good reference for what I want to do. 好的,对于任何想要帮助的人,我现在对我想做的事情有一个非常好的参考。 I figured out the second part of this before the first, the second part being text quoting. 我在第一部分之前找到了第二部分,第二部分是文本引用。 Here's the code: 这是代码:

$re = "/^^(>[a-zA-Z0-9_ ]*$)/mi";
$subst = "<p class=\"quote\">$1</p>";
foreach ($boards as $x) {
  $x = $db->real_escape_string($x);
  $db->real_query("SELECT * FROM posts_".$x." ORDER BY timestamp DESC LIMIT 1");
  $res = $db->use_result();
  while ($row = $res->fetch_assoc()) {
    $str = str_replace("\r\n", "\n", $row['content']);
    $str = str_replace("\r", "\n", $str);
    $content = preg_replace($re, $subst, $str);
    echo "<p style=\"font-size:110%;\">/$x/ - </p>" . "<p>" . nl2br($content) . "<a href=\"" . "$x/" . $row['op'] . "\"> [reply]</a></p><br/>";
  }
}

FINAL EDIT: I got the first part now as well. 最终编辑:我现在也得到了第一部分。

https://regex101.com/r/kC1bM7/3 https://regex101.com/r/kC1bM7/3

Don't have the closing anchor $ , the remaining text breaks that. 没有结束锚$ ,剩下的文本打破了。 The quantifier will find all additional numbers. 量词将找到所有附加数字。 (You also could take off the leading anchor ^ if the >>number is allowed anywhere in the string, in both examples it was leading so I think that is constant?). (你也可以起飞的领先锚^如果>>number允许在字符串中的任何地方,在这两个例子,这是领导,所以我认为这是不变的?)。

Try: 尝试:

^(>>\d+)

Demo: https://regex101.com/r/kC1bM7/2 演示: https//regex101.com/r/kC1bM7/2

You also should encode the > s as their entity if you are using that in HTML, &gt; 如果您在HTML中使用,则还应将> s编码为其实体, &gt; .

PHP Usage I'd use something like: PHP用法我会使用类似的东西:

preg_replace('/^>>(\d+)/', '&gt;&gt;$1', $string);

Also not the greater than symbol has no special meaning in regex so it doesn't need to be escaped. 也不是大于符号在正则表达式中没有特殊含义所以它不需要被转义。

暂无
暂无

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

相关问题 我不知道如何将iframe链接到按钮 - I Can't Figure Out How To Link an iframe to a Button 我是 nodejs 的新手,我收到了参考错误:io is not defined ,我想不通 - I am new to nodejs and I'm getting reference error: io is not defined ,I can't figure it out 我有编程经验,但是对Javascript还是陌生的,无法弄清楚为什么我的代码无法运行 - I'm experienced with programming but new to Javascript, can't figure out why my code won't run 我不明白为什么我会无限循环 - I can't figure out why I'm getting infinite loop 我无法弄清楚我错过了什么,除了一个选项,代码100%工作 - I can't figure out what I'm missing, code works 100% except for one option 我不知道我在使用 getDay() 时做错了什么 - I can't figure out what I'm doing wrong with getDay() 我不知道为什么我在这里得到“无法读取未定义的属性&#39;长度&#39;的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 我不知道为什么会出现 ReferenceError: fileUploaded is not defined - I can't figure out why I'm getting a ReferenceError: fileUploaded is not defined 我正在尝试循环复选框和收音机,但我无法弄清楚? - I'm trying to loop checkboxes and radios but I can't figure it out? 试图解决许多制作硬币的方法,但我无法弄清楚我的逻辑在哪里有缺陷 - Trying to solve number of ways of making coins, but I can't figure out where my logic is flawed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM