문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

Here is one option using regular expressions:

$filename = "a.jpg3c.pngyes.jpg";
$regex = '/\.(jpg|png|gif)(?!$)/';
$filename = preg_replace($regex, ".$1;", $filename);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top