[英]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, >
如果您在HTML中使用,则还应将
>
s编码为其实体, >
. 。
PHP Usage I'd use something like: PHP用法我会使用类似的东西:
preg_replace('/^>>(\d+)/', '>>$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.