If you just want to change from image:name.jpg
to image_name.jpg
you can use something like:
<?php
$name = "image:name.jpg";
$name = preg_replace('/\:/','_',$name);
echo $name;
This will echo image_name.jpg
You can also use str_replace
which should be faster then preg_replace
, according to this entry. Which would be something like:
<?php
$name = "image:name.jpg";
$name = str_replace(':','_',$name);
echo $name;
And this will also echo image_name.jpg
If you want to replace the whole URL maintaining the :
in http://....
you can use \K
.
According to Maroun Maroun:
\K
tells the engine to pretend that the match attempt started at
this position.
With the following code:
<?php
$name = '<img src="http://domainname.com/image:name.jpg">';
$name = preg_replace('/[^http:]\K(:)/', '_', $name);
echo $name;
It will echo <img src="http://domainname.com/image_name.jpg">
. You can see a working example here.