This question already has answers here:
PHP, get file name without file extension
(18 answers)
Closed 4 years ago.
I'm looking for a way to get a substring in php. Given string is:
"abc/def/ghi/name.extension"
I only want to have 'name', and the number of forward slashes is not known and not the same number either. Tried it with substring, but I get stuck in the elimination of the /
Anyone an idea how to solve this?
Help is greatly appreciated!
You can use pathinfo:
$str = "abc/def/ghi/name.extension";
$name = \pathinfo( $str, \PATHINFO_FILENAME );
Try this. First split the string into an array with explode () and then split the last element by the dot.
$a = 'abc/def/ghi/name.extension';
$b = explode ('/', $a);
$b = $b[count ($b) - 1];
$c = explode ('.', $b)[0];
You can try this for filename.
$filename = pathinfo("abc/def/ghi/name.extension", PATHINFO_FILENAME);
echo $filename;
and this for file extension.
$ext = pathinfo("abc/def/ghi/name.extension", PATHINFO_EXTENSION);
echo $ext;
Related
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 6 years ago.
How do I split a string by . delimiter in PHP? For example, if I have the string "a.b", how do I get "a"?
explode does the job:
$parts = explode('.', $string);
You can also directly fetch parts of the result into variables:
list($part1, $part2) = explode('.', $string);
explode('.', $string)
If you know your string has a fixed number of components you could use something like
list($a, $b) = explode('.', 'object.attribute');
echo $a;
echo $b;
Prints:
object
attribute
$string_val = 'a.b';
$parts = explode('.', $string_val);
print_r($parts);
Documentation: explode
The following will return you the "a" letter:
$a = array_shift(explode('.', 'a.b'));
Use:
explode('.', 'a.b');
explode
$array = explode('.',$string);
Returns an array of split elements.
To explode with '.', use:
explode('\\.', 'a.b');
This question already has answers here:
PHP explode the string, but treat words in quotes as a single word
(5 answers)
Closed 7 years ago.
I have a string like this |casio|watch|men| in an xml. I want to explode this and i need output like this : $brand = "casio"; $cat = "watch";.
i used these codes but it doesnt helped me
<?php
$string = '|casio|watch|men|';
$string = explode('-',$string);
var_dump($string);
echo $string[0];
echo $string[1];
?>
your trying to separate the string using '-' while your string doesnt contain any... in your case you should explode the using '|' example
$string = explode('|',$string);
This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 8 years ago.
http://www.example.com/hu/link
Using PHP how can I only keep the hu? Please note that the link is only one variable, it may be anything
You can use explode
$exploded = explode('/', 'http://www.example.com/hu/link');
$value = $exploded[3];
One solution is this:
$split = explode("/", $_SERVER["REQUEST_URI"]);
echo $split[1];
$url = "http://www.example.com/hu/link";
$split_url = explode('/', $url);
echo $split_url[3];
Output:
hu
You can use a regular expression preg_match like this:
$url = 'http://www.example.com/hu/link';
preg_match('/.*www.*\/(.*)\//i',$url, $matches);
print_r($matches[1]);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - efficent way to get and remove first line in file
I need to remove first line form txt file using php. Could you show me example code how to do it? I know that it's easy but I don't know php:) Thanks!
You could do this:
$file = file_get_contents(SOME_FILE);
$arr = explode('\n\r', $file);
if (isset($arr[0])) unset ($arr[0]);
$string = implode('\n\r', $arr);
file_put_contents(SOME_FILE, $string);
You could have used the search function at least. You would have found this:
$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 6 years ago.
How do I split a string by . delimiter in PHP? For example, if I have the string "a.b", how do I get "a"?
explode does the job:
$parts = explode('.', $string);
You can also directly fetch parts of the result into variables:
list($part1, $part2) = explode('.', $string);
explode('.', $string)
If you know your string has a fixed number of components you could use something like
list($a, $b) = explode('.', 'object.attribute');
echo $a;
echo $b;
Prints:
object
attribute
$string_val = 'a.b';
$parts = explode('.', $string_val);
print_r($parts);
Documentation: explode
The following will return you the "a" letter:
$a = array_shift(explode('.', 'a.b'));
Use:
explode('.', 'a.b');
explode
$array = explode('.',$string);
Returns an array of split elements.
To explode with '.', use:
explode('\\.', 'a.b');