替换 php 中连字符和点之间的部分字符串

Replace part of a string between hyphen and dot in php

我需要替换 php 中两个字符(最后一个连字符和最后一个点)之间的部分字符串。有什么想法吗?

$string = 'http://example.com/wp-content/uploads/2015/07/2b146fdab6c33eb5ca12efe61424427b-1000x750.jpg' 

数字可能会有所不同,1000x750 只是一个示例。

$newstring = 'http://example.com/wp-content/uploads/2015/07/2b146fdab6c33eb5ca12efe61424427b-600x400.jpg'

谢谢!

您可能会发现此脚本很有用:

// The string you want to replace between the two characters
$replacewith = "600x400";

// The example string you provided
$string = $newstring = 'http://example.com/wp-content/uploads/2015/07/2b146fdab6c33eb5ca12efe61424427b-1000x750.jpg';

// Determine the start/end position of the last hyphen and period characters
$start = strrpos($string, "-");
$end = strrpos($string, ".");

// If both the hyphen and character are found, then do the replacement
if ($start && $end) {

    $newstring = substr($string, 0, $start+1) . $replacewith . substr($string, $end);

}

echo $newstring;

// Outputs http://example.com/wp-content/uploads/2015/07/2b146fdab6c33eb5ca12efe61424427b-600x400.jpg