<[a-z][^<>]*?\s(?:href|src)' . '\s*=\s*)(?["\'])(?
.*?)\k#is'; public function __construct( private AddressMatch $address_match ) { } public function replace( string $content, string $link_url, string $replacement_url ): string { return (string) preg_replace_callback( self::ATTRIBUTE_PATTERN, fn( array $attribute ): string => $this->replaced_attribute( $attribute, $link_url, $replacement_url ), $content ); } /** @param array{0: string, prefix: string, quote: string, address: string} $attribute */ private function replaced_attribute( array $attribute, string $link_url, string $replacement_url ): string { $written_address = $attribute['address']; if ( ! $this->address_match->is_same( $written_address, $link_url ) ) { return $attribute[0]; } $quote = $attribute['quote']; return $attribute['prefix'] . $quote . esc_attr( $replacement_url ) . $quote; } }