@@ -152,29 +152,43 @@ protected function setHost(string $link = null): void
152
152
153
153
protected function setHostInContent (string $ host = null ): void
154
154
{
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 ;
160
157
}
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 };
166
181
}
167
182
}
168
183
169
184
public function getHostFromLink (): ?string
170
185
{
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 ());
176
190
177
- return $ result ;
191
+ return $ partsUrl [ ' scheme ' ]. " :// " . $ partsUrl [ ' host ' ] ;
178
192
}
179
193
180
194
public function getValue (string $ name ): ?string
0 commit comments