Skip to content

Commit 205dec2

Browse files
authored
Fix: Error preg_replace(), add all other possible replacements for relative links. (#427)
Close #425
1 parent 7c206ea commit 205dec2

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/FeedIo/Feed/Node.php

+30-16
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,43 @@ protected function setHost(string $link = null): void
152152

153153
protected function setHostInContent(string $host = null): void
154154
{
155-
if (property_exists($this, 'content')){
156-
if (!is_null($host) && !is_null($this->content)) {
157-
$this->content = preg_replace('!(<*\s*[^>]*)(href=)(.?)(\/[^\/])!','\1 href=\3'.$host.'\4', $this->content );
158-
$this->content = preg_replace('!(<*\s*[^>]*)(src=)(.?)(\/[^\/])!','\1 src=\3'.$host.'\4', $this->content );
159-
}
155+
if (is_null($host)) {
156+
return;
160157
}
161-
if (property_exists($this, 'description')){
162-
if (!is_null($host) && !is_null($this->description)) {
163-
$this->description = preg_replace('!(<*\s*[^>]*)(href=)(.?)(\/[^\/])!','\1 href=\3'.$host.'\4', $this->description );
164-
$this->description = preg_replace('!(<*\s*[^>]*)(src=)(.?)(\/[^\/])!','\1 src=\3'.$host.'\4', $this->description );
165-
}
158+
// Replaced links like href="/aaa/bbb.xxx"
159+
$pattern = '(<\s*[^>]*)(href=|src=)(.?)(\/[^\/])(?!(.(?!<code))*<\/code>)';
160+
$this->pregReplaceInProperty('content', $pattern, '\1\2\3'.$host.'\4');
161+
$this->pregReplaceInProperty('description', $pattern, '\1\2\3'.$host.'\4');
162+
163+
$itemFullLink = $this->getLink();
164+
$itemLink = implode("/", array_slice(explode("/", $itemFullLink), 0, -1))."/";
165+
166+
// Replaced links like href="#aaa/bbb.xxx"
167+
$pattern = '(<\s*[^>]*)(href=|src=)(.?)(#)(?!(.(?!<code))*<\/code>)';
168+
$this->pregReplaceInProperty('content', $pattern, '\1\2\3'.$itemFullLink.'\4');
169+
$this->pregReplaceInProperty('description', $pattern, '\1\2\3'.$itemFullLink.'\4');
170+
171+
// Replaced links like href="aaa/bbb.xxx"
172+
$pattern = '(<\s*[^>]*)(href=|src=)(.?)(\w+\b)(?![:])(?!(.(?!<code))*<\/code>)';
173+
$this->pregReplaceInProperty('content', $pattern, '\1\2\3'.$itemLink.'\4');
174+
$this->pregReplaceInProperty('description', $pattern, '\1\2\3'.$itemLink.'\4');
175+
}
176+
177+
public function pregReplaceInProperty(string $property, string $pattern, string $replacement): void
178+
{
179+
if (property_exists($this, $property) && !is_null($this->{$property})) {
180+
$this->{$property} = preg_replace('~'.$pattern.'~', $replacement, $this->{$property}) ?? $this->{$property};
166181
}
167182
}
168183

169184
public function getHostFromLink(): ?string
170185
{
171-
if (!is_null($this->getLink())) {
172-
$partsUrl = parse_url($this->getLink());
173-
$result = $partsUrl['scheme']."://".$partsUrl['host'];
174-
} else
175-
$result = null;
186+
if (is_null($this->getLink())) {
187+
return null;
188+
}
189+
$partsUrl = parse_url($this->getLink());
176190

177-
return $result;
191+
return $partsUrl['scheme']."://".$partsUrl['host'];
178192
}
179193

180194
public function getValue(string $name): ?string

0 commit comments

Comments
 (0)