[PHP-WEBMASTER] [web-news] master: Stop word-wrapping in quotes from breaking links

Author: Tim Düsterhus (TimWolla)
Committer: Derick Rethans (derickr)
Date: 2026-10-06T11:58:01+01:00

Commit: Stop word-wrapping in quotes from breaking links · php/web-news@6375a2b · GitHub
Raw diff: https://github.com/php/web-news/commit/6375a2b48b7fb42724ad58c80691d7c98caa5f5f.diff

Stop word-wrapping in quotes from breaking links

If an URL appears in a quote and is quite lengthy, `wordwrap()` will break the
line between the `<a` and the `href="…"` due to the space. Within quotes a
`&gt;` will be inserted, breaking the markup.

Fix this by “stashing away” HTML tags before wordwrapping and restoring them
after. This might still result in weird / unexpected linebreaks for HTML tags
in the rendered version, but not break them.

Changed paths:
  M article.php

Diff:

diff --git a/article.php b/article.php
index 507b01a..4b133f0 100644
--- a/article.php
+++ b/article.php
@@ -317,8 +317,16 @@
     # Otherwise we're in a signature or not flowed
     else {
         if (!$insig && preg_match('/^((\s*\w*?&gt; ?)+)/', $line, $m)) {
+ $tags = [];
+ $line = preg_replace_callback('/<[^>]+>/', function ($matches) use (&$tags) {
+ $id = random_bytes(16);
+ $tags[$id] = $matches[0];
+
+ return $id;
+ }, $line);
+
             $level = substr_count($m[1], '&gt;') % 4;
- echo "<span class=\"quote$level\">", wordwrap($line, 100, "\n" . $m[1]), "</span>";
+ echo "<span class=\"quote$level\">", strtr(wordwrap($line, 100, "\n" . $m[1]), $tags), "</span>";
         } else {
             echo wordwrap($line, 100);
         }