Support » Plugin: Domain Sharding » Tip: modify replacing pattern for better results

  • Hi David,

    I suggest modifying the preg_match_all regex and the str_ireplace as shown below to achieve better results with replacements.
    Eg. your original solution only replaces the first image url in an srcset but mine replaces all image urls.
    It is true that fixing the .jpg extension in the pattern is not that great but the results are fine for me as I use jpg files in my posts. You can improve it if you like.

    Here are the diffs of the two modifications for wp-content/plugins/domain-sharding/domain-sharding.php:

    @@ -334,7 +334,7 @@
     	{
     		if ( $buffer != '' )
     		{
    -			preg_match_all('/src\s*=\s*"([^"]+)"/sim', $buffer, $result, PREG_SET_ORDER);
    +			preg_match_all('/(https?:\/\/[^"\']+?\.jpg)/sim', $buffer, $result, PREG_SET_ORDER);
     
     			$urls=array();
     			foreach( $result as $row )

    and

    @@ -394,7 +394,7 @@
     					$url = str_replace('http://', '//', $url);
     				}
     
    -				$buffer = str_ireplace( '"'.$original, '"'.$url, $buffer );
    +				$buffer = str_ireplace( ''.$original, ''.$url, $buffer );
     			}
     		}

    Additional feature of the modification that even wen an AJAX call is made, the image urls are replaced! 🙂

    Cheers,
    Immánuel

  • The topic ‘Tip: modify replacing pattern for better results’ is closed to new replies.