How get filename from path php? - php

I have array with paths:
array (size=27692)
0 => string './users/58/576709/16376/16376.mp4'
(length=34)
1 => string './users/58/578974/45475/45475.mp4'
(length=34)
//...
How i can get for example 16376.mp4?

This should work for you:
<?php
$str = "./users/58/576709/16376/16376.mp4";
echo basename($str);
?>
Output:
16376.mp4
And if you have a Array, as an example:
<?php
$arr = array("./users/58/576709/16376/16376.mp4", "./users/58/578974/45475/45475.mp4");
foreach($arr as $v)
echo basename($v) . "<br />";
?>
Output:
16376.mp4
45475.mp4

Try this
$str = "./users/58/576709/16376/16376.mp4";
echo end(explode('/', $str));

$path = '/www/public_html/index.html';
$filename = substr(strrchr($path, "/"), 1);
Could also work so for array.
foreach($filepaths as $path){
$filename[] = substr(strrchr($path, "/"), 1);
}
This would give you a new array containing only the filename

This can be done using the following code:
$array = array(
0 => '/users/58/576709/16376/16376.mp4',
1 => '/users/58/576709/16376/16377.mp4'
);
echo $array[1];
$array = explode('/', $array[1]);
echo $array[sizeof($array) - 1];
Last line would print the filename

Related

check the same char in strg and remove and split from underscore in PHP?

Hello I have one string "22A_n22A" and i want to remove the same character from this string and want to split from "_" to two string like 22A and n22A.
i tried it but did not get any solution.
final out put i want like 22A_n22A to (n)22AA
<?php
function conti($str,$case_sensitive = false) {
//if($com1 = preg_replace("/\b(\w+)\s+\\1\b/i", "$1", $newval))
if(strcmp($case_sensitive , $case_sensitive) === 0 )
{
$pieces = explode("_", $str);
$str1 = $pieces[0];
$str2 = $pieces[1];
$ary1 = str_split($str1);
$ary2 = str_split($str2);
if (isset($case_sensitive))
{
$ary1 = array_map('strtolower',$ary1);
$ary2 = array_map('strtolower',$ary2);
}
$com = implode('',array_intersect($ary1,$ary2));
$diff = implode('',array_merge(array_diff($ary1, $ary2),array_diff($ary2, $ary1)));
$int = (int) filter_var($com, FILTER_SANITIZE_NUMBER_INT);
$onlystr = preg_replace('/\d/', '', $com);
$newval= '('.$diff.')'.$int.$onlystr.$onlystr;
// $com1 = preg_replace("/\b(\w+)\s+\\1\b/i", "$1", $str);
echo '<pre><h1>';
echo 'new value:';
print_r($newval);
echo "<br>";
echo "<br>";
echo "<br>";
echo 'new value:';
print_r($com);
echo "<br>";
echo 'new diff:';
print_r($diff);
echo '</pre>';
return $newval;
}
else {
echo '<pre><h1>';
echo 'oldvalue:';
print_r($str);
echo '</pre>';
return $str;
}
}
echo(conti('71A_n71A'));
// echo(conti('66A_n66A'));
?>
As stated in your comments your requirement is to:
Split the string on _
Pluck the last character from the first chunk of the string and append that to the end of the second string
Wrap the first character of the second chunk of the string in parentheses
For example, 65A_n66A becomes (n)66AA.
You can do this with by performing explode() on the original string, extracting the parts you need and piecing them back together in your desired format:
function format($string) {
list($first, $second) = explode('_', $string);
return sprintf('(%s)%s%s',
$second[0],
substr($second, 1),
$first[strlen($first) - 1]
);
}
This yields:
echo format('22A_n22A'); // (n)22AA
echo format('11A_n22B'); // (n)22BA
echo format('65A_n66A'); // (n)66AA
Hope this helps :)

PHP preg_replace string path given

I have issue with preg_replace function in PHP. I cant figure out a pattern and a replacement.
I have this two strings and some code:
$dirname1 = 'hdadas/dasdad/dasd/period_1min';
$dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';
$pieces1 = explode('/', $dirname1);
$pieces2 = explode('/', $dirname2);
$dirname1 = end($pieces1); // output will be period_1min
$dirname2 = end($pieces2); // output will be abcdrfg.php
$output = preg_replace($pattern, $replacement, $dirname1); // or (..,..,$dirname2
echo $output; // i need 1min(without period_) or abcdrfg (without .php)
UPD:
function Cat($dirname)
{
$name = explode('/', $dirname);
$pattern = ???;
$replacement = ???;
return preg_replace($pattern, $replacement, $dirname1);
}
print(Cat('hdadas/dasdad/dasd/period_1min'))); // output need 1min only
print(Cat('hdadas/dasdad/dasd/period_1min/abcdrfg.php'))); // output need abcdrfg only
This should work for you:
(Here I use basename() to get only the last part of the path, then I use pathinfo() to get the last part without the extension. After this I just use preg_replace() to replace everything before the underscore with an empty string)
<?php
$dirname1 = "hdadas/dasdad/dasd/period_1min";
$dirname2 = "hdadas/dasdad/dasd/period_1min/abcdrfg.php";
$dirname1 = pathinfo(basename($dirname1))["filename"];
$dirname2 = pathinfo(basename($dirname2))["filename"];
echo $output = preg_replace("/(.*)_/", "", $dirname1);
?>
output:
1min
abcdrfg
How about this regex /^.+_|\.[^.]+$/:
$dirname1 = 'hdadas/dasdad/dasd/period_1min';
$dirname2 = 'hdadas/dasdad/dasd/period_1min/abcdrfg.php';
$pieces1 = explode('/', $dirname1);
$pieces2 = explode('/', $dirname2);
$dirname1 = end($pieces1); // output will be period_1min
$dirname2 = end($pieces2); // output will be abcdrfg.php
$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname1); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)
$output = preg_replace('/^.+_|\.[^.]+$/', '', $dirname2); // or (..,..,$dirname2
echo $output,"\n"; // i need 1min(without period_) or abcdrfg (without .php)
Output:
1min
abcdrfg

php match a string from other string

I have a string as $test = 'aa,bb,cc,dd,ee' and other string as $match='cc'. I want the result as $result='aa,bb,dd,ee'.
I am not able to get te result as desired as not sure which PHP function can give the desired output.
Also if I have a string as $test = 'aa,bb,cc,dd,ee' and other string as $match='cc'. I want the result as $match=''. i.e if $match is found in $test then $match value can be skipped
Any help will be really appreciated.
You can try with:
$test = 'aa,bb,cc,dd,ee';
$match = 'cc';
$output = trim(str_replace(',,', ',', str_replace($match, '', $test), ','));
or:
$testArr = explode(',', $test);
if(($key = array_search($match, $testArr)) !== false) {
unset($testArr[$key]);
}
$output = implode(',', $testArr);
Try with preg_replace
$test = 'aa,bb,cc,dd,ee';
$match ='cc';
echo $new = preg_replace('/'.$match.',|,'.$match.'$/', '', $test);
Output
aa,bb,dd,ee
$test = 'aa,bb,cc,dd,ee';
$match='cc';
echo trim(str_replace(',,', ',' , str_replace($match,'',$test)),',');
DEMO
Try this:
$test = 'aa,bb,cc,dd,ee';
$match = 'cc';
$temp = explode(',', $test);
unset($temp[ array_search($match, $temp) ] );
$result = implode(',', $temp);
echo $result;

str_replace doesn't work with foreach

My code:
$str = array(
'{$string1}' => 'anything2',
'{$string2}' => 'something1',
'{$string3}' => '...'
);
$final = "";
$text = $_POST['content'];
foreach( $str as $key => $val ) {
$final = str_replace($key, $val, $text);
}
My $text ofc. has {string1} , {string2} and {string3} itself, but it doesn't replace it with the values given in the array.
Why its not working?
This code does exactly what you need (without any extra loops):
$final = strtr($_POST['content'], $str);
use
$final = str_replace('{'.$key.'}', $val, $text);
Ref : http://php.net/manual/en/function.str-replace.php
Maybe the different enconding, try this:
$text = utf8_decode($_POST['content']);// or utf8_encode
before loop;
Good lucky!

How to get quickly two data from this String data?

There is this String data from the column value of a table column : /var/www/imfmobile/photoj2meupload/7455575/photo32.png
I want to get the 7455575 and the photo32.png substring's. How to achieve that quickly ?
use explode to split the string at /:
$parts = explode('/',$mystring);
and then just use array_pop to get the values:
$filename = array_pop($parts);
$foldername = array_pop($parts);
$str = '/var/www/imfmobile/photoj2meupload/7455575/photo32.png';
$arr = explode('/', $str);
echo end($arr); // photo32.png
echo prev($arr); // 7455575
You might want to look at the pathinfo() and basename() functions:
$path_parts = pathinfo('/var/www/imfmobile/photoj2meupload/7455575/photo32.png');
echo basename( $path_parts['dirname'] ) . "\n";
echo $path_parts['basename'] . "\n";
Will output:
7455575
photo32.png

Categories