Reputation: 133
i want to change :
to _
like
<img src="http://domainname.com/image:name.jpg">
output
<img src="http://domainname.com/image_name.jpg">
I have this regex but it doesn't work:
$f1content = preg_replace('/\<img src="\/\/(.*\.jpg"|.jpeg"|.gif" |.png"|.jpg">|.jpeg">|.gif">|.png">)/','/1',$content)
$result = str_replace(':', '_', $f1content);
Upvotes: 0
Views: 167
Reputation: 874
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.
Upvotes: 3