Skip to content

Commit

Permalink
Fix: Error preg_replace(), add all other possible replacements for re…
Browse files Browse the repository at this point in the history
…lative links. (#427)

Close #425
  • Loading branch information
IgorA100 authored Sep 6, 2024
1 parent 7c206ea commit 205dec2
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/FeedIo/Feed/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,43 @@ protected function setHost(string $link = null): void

protected function setHostInContent(string $host = null): void
{
if (property_exists($this, 'content')){
if (!is_null($host) && !is_null($this->content)) {
$this->content = preg_replace('!(<*\s*[^>]*)(href=)(.?)(\/[^\/])!','\1 href=\3'.$host.'\4', $this->content );
$this->content = preg_replace('!(<*\s*[^>]*)(src=)(.?)(\/[^\/])!','\1 src=\3'.$host.'\4', $this->content );
}
if (is_null($host)) {
return;
}
if (property_exists($this, 'description')){
if (!is_null($host) && !is_null($this->description)) {
$this->description = preg_replace('!(<*\s*[^>]*)(href=)(.?)(\/[^\/])!','\1 href=\3'.$host.'\4', $this->description );
$this->description = preg_replace('!(<*\s*[^>]*)(src=)(.?)(\/[^\/])!','\1 src=\3'.$host.'\4', $this->description );
}
// Replaced links like href="/aaa/bbb.xxx"
$pattern = '(<\s*[^>]*)(href=|src=)(.?)(\/[^\/])(?!(.(?!<code))*<\/code>)';
$this->pregReplaceInProperty('content', $pattern, '\1\2\3'.$host.'\4');
$this->pregReplaceInProperty('description', $pattern, '\1\2\3'.$host.'\4');

$itemFullLink = $this->getLink();
$itemLink = implode("/", array_slice(explode("/", $itemFullLink), 0, -1))."/";

// Replaced links like href="#aaa/bbb.xxx"
$pattern = '(<\s*[^>]*)(href=|src=)(.?)(#)(?!(.(?!<code))*<\/code>)';
$this->pregReplaceInProperty('content', $pattern, '\1\2\3'.$itemFullLink.'\4');
$this->pregReplaceInProperty('description', $pattern, '\1\2\3'.$itemFullLink.'\4');

// Replaced links like href="aaa/bbb.xxx"
$pattern = '(<\s*[^>]*)(href=|src=)(.?)(\w+\b)(?![:])(?!(.(?!<code))*<\/code>)';
$this->pregReplaceInProperty('content', $pattern, '\1\2\3'.$itemLink.'\4');
$this->pregReplaceInProperty('description', $pattern, '\1\2\3'.$itemLink.'\4');
}

public function pregReplaceInProperty(string $property, string $pattern, string $replacement): void
{
if (property_exists($this, $property) && !is_null($this->{$property})) {
$this->{$property} = preg_replace('~'.$pattern.'~', $replacement, $this->{$property}) ?? $this->{$property};
}
}

public function getHostFromLink(): ?string
{
if (!is_null($this->getLink())) {
$partsUrl = parse_url($this->getLink());
$result = $partsUrl['scheme']."://".$partsUrl['host'];
} else
$result = null;
if (is_null($this->getLink())) {
return null;
}
$partsUrl = parse_url($this->getLink());

return $result;
return $partsUrl['scheme']."://".$partsUrl['host'];
}

public function getValue(string $name): ?string
Expand Down

0 comments on commit 205dec2

Please sign in to comment.