I have this code :
$value = 'daryaa-manmarziyan-(VideoStatus.Net).mp4';
$final_value = preg_replace("/[^a-zA-Z0-9_.]/", "", $value);
echo $final_value; exit();
Output is : daryaamanmarziyanVideoStatus.Net.mp4
I want output daryaamanmarziyanVideoStatusNet.mp4
All unnecessary dot(.) will remove What can i do ?
You should have to use
$value = 'daryaa-manmarziyan-(VideoStatus.Net).mp4';
$fileNames = preg_replace(array("/[^a-zA-Z0-9_.]/", "/\.(?=.*\.)/"), "", $fileName);
echo $final_value; exit();
Output will come
daryaamanmarziyanVideoStatusNet.mp4
Change your preg_replace
call to this:
$final_value = preg_replace(array("/[^a-zA-Z0-9_.]/", "/\.(?=.*\.)/"), "", $value);
The second replace string (/\.(?=.*\.)/
) looks for a .
which is followed by another .
and replaces that with an empty string.
Output for your sample data:
daryaamanmarziyanVideoStatusNet.mp4
Use a regex with an alternation:
preg_replace("/[^\w.]+|\.(?=[^.]*\.)/", "", $value);
^^^^^^^^^^^^^^
See the PHP demo online and an online regex demo.
The \.(?=[^.]*\.)
pattern matches any .
with \.
that is followed with any 0+ chars other than .
followed with .
.
Also, [A-Za-z0-9_]
is equal to \w
if you do not use the u
modifier, thus, you may shorten the pattern a bit.
Details
[^\w.]+
- 1 or more chars other than word (A-Za-z0-9_
) chars and .
|
- or\.
- a dot...(?=[^.]*\.)
- (a positive lookahead that, immediately to the right of the current location, makes sure that dot is) followed with[^.]*
- 0+ chars other than .
\.
- a dot.