Question

How can I add divider ; in the following variable which contains string

I have string like this:

$filename = "a.jpg3c.pngyes.jpg";

I would like to have something like

a.jpg;3c.png;yes.jpg

This string is created when I select multiple files to upload.

Is regex only solution in here?

Was it helpful?

Solution

Regex is not the only solution! Perhaps you can use str_replace() instead of regex.

$filenames = "a.jpg3c.pngyes.jpg";

$img_extensions = array(".png", ".jpg", ".gif");
$semicolon_additions = array(".png;", ".jpg;", ".gif;");

$newfilenames = str_replace($img_extensions, $semicolon_additions, $filenames);

http://php.net/manual/en/function.str-replace.php

Edit: In your particular case, I would add in the semicolon at the end of the filename inside of your loop.

OTHER TIPS

Here is one option using regular expressions:

$filename = "a.jpg3c.pngyes.jpg";
$regex = '/\.(jpg|png|gif)(?!$)/';
$filename = preg_replace($regex, ".$1;", $filename);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top