Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将字符插入字符串中的特定位置_Php_Str Replace - Fatal编程技术网

PHP将字符插入字符串中的特定位置

PHP将字符插入字符串中的特定位置,php,str-replace,Php,Str Replace,我有字符串(链接) 我想在这个字符串中插入某些字符{s} 家长:http://i.imgur.com/filename{character}.扩展名 之前:http://i.imgur.com/7k8t8pC.png 之后:http://i.imgur.com/7k8t8pCs.png您可以使用substr\u replace的小技巧: $newstring = substr_replace( "http://i.imgur.com/7k8t8pC.png", "s", 26, 0); 编辑,

我有字符串(链接)

我想在这个字符串中插入某些字符{s}

家长:
http://i.imgur.com/filename{character}.扩展名

之前:
http://i.imgur.com/7k8t8pC.png


之后
http://i.imgur.com/7k8t8pCs.png

您可以使用substr\u replace的小技巧:

$newstring = substr_replace( "http://i.imgur.com/7k8t8pC.png", "s", 26, 0);
编辑,稍作解释: 它允许用另一个字符串替换字符串的一部分。传递原始字符串、要插入的字符串、起始点以及要替换的字符数。但是,如果您传递了0,则会在给定位置将第二个字符串插入第一个字符串。

您可以使用:

结果:

http://i.imgur.com/7k8t8pCs.png
http://i.imgur.com/7k8t8pCs.png

或使用:

结果:

http://i.imgur.com/7k8t8pCs.png
http://i.imgur.com/7k8t8pCs.png

我犹豫是否发布此内容,但由于您使用的是路径,而不是:

$info = pathinfo($string);
$result = $info['dirname'] . "/" . $info['filename'] . "s." . $info['extension'];
这个怎么样:

function addstring($ch,$string){  
    $array = explode('/',$string);
    $name = explode('.',end($array));
    array_pop($array);
    $new = implode('/',$array);
    return $new.'/'.$name[0].$ch.'.'.$name[1];
}
var_dump(addstring('CHAR','http://i.imgur.com/7k8t8pC.png'));

因此,您有一个字符和一个位置,您想在这个位置插入这个字符,还是字符串已经是这种格式:
http://i.imgur.com/7k8t8pC{character}.png
?我有以下格式的字符串:
http://i.imgur.com/7k8t8pC.png
(字符串前)它总是在文件名的末尾吗?所以你想在文件扩展名之前插入
s
?str_replace().png和{character}。当str_replace()非常整洁、简单且灵活时,pngdont使用正则表达式。