How to divide string in php - php

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);

Related

PHP: How to explode string

I have a variable that stores the location of a temp file:
$file = 'C:\xampp\htdocs\temp\filename.tmp';
How can I explode all this to get filename (without the path and extension)?
Thanks.
Is not the best code but if you confident that this path will be similar and just file name will be different you can use this code:
$str = 'C:\xampp\htdocs\temp\filename.tmp';
$arrayExplode = explode("\\", $str);
$file = $arrayExplode[count($arrayExplode)-1];
$filename = explode('.', $file);
$filename = $filename[0];
echo $filename;
Advice: Watch out on the path contain "n" like the first letter after the backslash. It could destroy your array.
You should use the basename function, it's meant specifically for that.

Preg Split not working if use variable in pattern

I have string like this and i want to split with matched pattern
$string = '[(123,234,34)tes2t.jpg*#*test.jpg]';//dynamic no of files
so if i try like this,
$keywords =preg_split( "/(?<=test.jpg)/", $string,'-1');
it is working but i need to pass variable in pattern
$file = 'test.jpg';
$keywords =preg_split( "/(?<=$file)/", $string,'-1');
This is not working.please help.
Thanks in advance.
$file = 'test.jpg';
$keywords =preg_split( '/(?<='.$file.')/', $string,'-1');

PHP regex for image name with numbers

I have images with names such as:
img-300x300.jpg
img1-250x270.jpg
These names will be stored in a string variable. My image is in Wordpress so it will be located at e.g.
mywebsite.com/wp-content/uploads/2012/11/img-300x300.jpg
and I need the string to be changed to
mywebsite.com/wp-content/uploads/2012/11/img.jpg
I need a PHP regular expression which would return img.jpg and img1.jpg as the names.
How do I do this?
Thanks
Addition
Sorry guys, I had tried this but it didn't work
$string = 'img-300x300.jpg'
$pattern = '[^0-9\.]-[^0-9\.]';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
You can do this using PHP native functions itself.
<?php
function genLink($imagelink)
{
$img1 = basename($imagelink);
$img = substr($img1,0,strrpos($img1,'-')).substr($img1,strpos($img1,'.'));
$modifiedlink = substr($imagelink,0,strrpos($imagelink,'/'))."/".$img;
return $modifiedlink;
}
echo genLink('mywebsite.com/wp-content/uploads/2012/11/flower-img-color-300x300.jpg');
OUTPUT :
mywebsite.com/wp-content/uploads/2012/11/flower-img-color.jpg
You can do that as:
(img\d*)-([^.]*)(\..*)
and \1\3 will contain what you want:
Demo: http://regex101.com/r/vU2mD4
Or, replace (img\d*)-([^.]*)(\..*) with \1\3
May be this?
(\w+)-[^.]+?(\.\w+)
The $1$2 will give you what you want.
search : \-[^.]+
replace with : ''
(.[^\-]*)(?:.[^\.]*)\.(.*)
group 1 - name before "-"
group 2 - extension. (everything after ".")
As long as there is only one - and one . then explode() should work great for this:
<?php
// array of image names
$images = array();
$images[] = 'img-300x300.jpg';
$images[] = 'img1-250x270.jpg';
// array to store new image names
$new_names = array();
// loop through images
foreach($images as $v)
{
// explode on dashes
// so we would have something like:
// $explode1[0] = 'img';
// $explode1[1] = '300x300.jpg';
$explode1 = explode('-',$v);
// explode the second piece on the period
// so we have:
// $explode2[0] = '300x300';
// $explode2[1] = 'jpg';
$explode2 = explode('.',$explode1[1]);
// now bring it all together
// this translates to
// img.jpg and img1.jpg
$new_names[] = $explode1[0].'.'.$explode2[1];
}
echo '<pre>'.print_r($new_names, true).'</pre>';
?>
That's an interesting question, and since you are using php, it can be nicely solved with a branch reset (a feature of Perl, PCRE and a few other engines).
Search: img(?|(\d+)-\d{3}x\d{3}|-\d{3}x\d{3})\.jpg
Replace: img\1.jpg
The benefit of this solution, compared with a vague replacement, is that we are sure that we are matching a file whose name matches the format you specified.

PHP how do i cut substring after last slash?

Hi i want to know how can i get substring from string after last slash?
In short i want to get the file name from path.
for example i got string like this:
test-e2e4/test-e2e4/test-e2e4/6.png
and i want to get 6.png how can i do that ?
I got dir only and the file name can be all format, it can be also something else then file
Or test-e2e4/test-e2e4/test-e2e4/aaaaa and want to get aaaaa
Regex maybe? Or maybe you know some nice functions which will do it for me ?
In addition to other replies, there's actually a function in PHP to do this: basename. Example:
$string = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$base = basename($string); // $base == '6.png';
$string = 'test-e2e4/test-e2e4/test-e2e4/aaaaa';
$base = basename($string); // $base == 'aaaaa'
$string = '6.png';
$base = basename($string); // $base == '6.png'
Full details here: http://php.net/basename
Do like this..
$yourstring = 'test-e2e4/test-e2e4/test-e2e4/6.png';
$val = array_pop(explode('/',$yourstring)); // 6.png
You can try explode and array_pop functions to work this out:
$str = 'test-e2e4/test-e2e4/test-e2e4/aaaaa.png';
$str = explode('/', $str);
$filename = array_pop($str);
echo $filename; //Output will be aaaaa.png
...Or you can use the following regex:
[^\/]*$
You don't need to use regex for this, you can use substr() to get a portion of the string, and strrpos() to specify which portion:
$full_path = "test-e2e4/test-e2e4/test-e2e4/6.png"
$file = substr( $full_path, strrpos( $full_path, "/" ) + 1 );
substr() returns a portion of the string, strrpos() tells it to start from the position of the last slash in the string, and the +1 excludes the slash from the return value.

PHP regular expression for directory

I need a regular expression that would take the string after the last forward slash.
For example, considering I have the following string:
C:/dir/file.txt
I need to take only the file.txt part (string).
Thank you :)
You don't need a regex.
$string = "C:/dir/file.txt";
$filetemp = explode("/",$string);
$file = end($filetemp);
Edited because I remember the latest PHP spitting errors out about chaining these types of functions.
If your strings are always paths you should consider the basename() function.
Example:
$string = 'C:/dir/file.txt';
$file = basename($string);
Otherwise, the other answers are great!
The strrpos() function finds the last occurrence of a string. You can use it to figure out where the file name starts.
$path = 'C:/dir/file.txt';
$pos = strrpos($path, '/');
$file = substr($path, $pos + 1);
echo $file;

Categories