If I have a string in the following format: location-cityName.xml how do I extract only the cityName, i.e. a word between - (dash) and . (period)?
Try this:
$pieces = explode('.', $filename);
$morePieces = explode('-', $pieces[0]);
$cityname = $morePieces[1];
Combine strpos() and substr().
$filename = "location-cityName.xml";
$dash = strpos($filename, '-') + 1;
$dot = strpos($filename, '.');
echo substr($filename, $dash, ($dot - $dash));
There are a few ways... this one is probably not as efficient as the strpos and substr combo mentioned above, but its fun:
$string = "location-cityName.xml";
list($location, $remainder) = explode("-", $string);
list($cityName, $extension) = explode(".", $remainder);
As i said... there are lots of string manipulation methods in php and you could do it many other ways.
Here's another way to grab the location as well, if you want:
$filename = "location-cityName.xml";
$cityName = preg_replace('/(.*)-(.*)\.xml/', '$2', $filename);
$location = preg_replace('/(.*)-(.*)\.xml/', '$1', $filename);
Here is a regular-expression–based approach:
<?php
$text = "location-cityName.xml";
if (preg_match("/^[^-]*-([^.]+)\.xml$/", $text, $matches)) {
echo "matched: {$matches[1]}\n";
}
?>
This will print out:
matched: cityName
Related
I have a directory containing folders like
baseurl/2-435435435_323423/
baseurl/5_435435435_32423/
baseurl/3_543543543_2342342/
Now i want to rename all the folders from their original name to new name i.e. truncate last part separated by '_'. new names would be
baseurl/2-435435435/
baseurl/5_435435435/
baseurl/3_543543543/
$path_from = 'E:/documents/';
if(file_exists($path_from)){
$files = scandir($path_from);
foreach($files as $key1 => $file) {
$newName = ? // I need this
rename($path_from.$file,$path_from.$newName);
}
}
Or Let me know if there is anyway in windows to rename batch without any script.
As you mention for getting only $newName, Just use substr and strrpos.
strrpos - Find the numeric position of the last occurrence of a string
$str = 'baseurl/3_543543543_2342342/';
$pos = strrpos($str, "_");
if ($pos === false){
//do nothing
}else
$str = substr($str, 0, $pos)."/";
echo $newName = $str; //baseurl/3_543543543/
You can make use of strstr
$path_from = 'E:/documents/';
if(file_exists($path_from)){
$files = scandir($path_from);
foreach($files as $key1 => $file) {
$newName = strstr($file, '_', true);
rename($path_from.$file,$path_from.$newName);
}
}
e.g.
$str = 'baseurl/2-435435435_323423/';
$imagePreFix = strstr($str, '_', true);
echo $imagePreFix;
OUTPUT:
baseurl/2-435435435
$newName = substr($file, 0, strrpos($file, '_'));
I have string like :
/home/kamal/public_html/clients/book/wp-content/uploads/wpcf7_uploads/1983322598/k.jpg
i need to get only
wp-content/uploads/wpcf7_uploads/1983322598/k.jpg
How can do that ?
$string = "/home/kamal/public_html/clients/book/wp-content/uploads/wpcf7_uploads/1983322598/k.jpg";
$exploded_string = explode("/", $string);
//$exploded_string is now an array of: home, kamal, public_html.... ...k.jpg
$n = array_search("wp-content", $exploded_string);
//$n is now the index of "wp-content" in the $exploded_string array
$new_path = array_slice($exploded_string, $n);
$new_path = implode("/", $new_path);
echo $new_path;
Suppose I have the following piece of code:
$myString = 'FilE.EXE';
strlower($myString);
I want to make the name minus its extension to lower case, but the code above will make the entire string into lower case. Is there a way I can just change the name without the extension? If so, what is the most dynamic way to accomplish this?
Desired output: 'file.EXE';
Using pathinfo
$myString = 'FilE.EXE';
$new_string = strtolower(pathinfo($myString, PATHINFO_FILENAME)) . '.' . pathinfo($myString, PATHINFO_EXTENSION);
echo $new_string;
You need to do something like this:
$string = "FilE.EXE";
list($name, $extension) = explode('.', $string);
$string = implode('.', array(strtolower($name), $extension));
Hope it helps.
do:
$myString = 'FilE.EXE';
$txt = strtolower( substr( $myString, 0, strrpos($myString, ".") ) )
.substr( $myString, strrpos($myString, "."), strlen($myString));
echo $txt; //gives file.EXE
You might want to use the pathinfo() function for that:
$myString = 'FilE.iNc.EXE';
$path_parts = pathinfo($myString);
$myNewString = implode('.', array(
strtolower($path_parts['filename']),
$path_parts['extension']
));
So it can ouput this:
file.inc.EXE
<?php
$myString = 'FilE.EXE';
$txt = strtolower( substr( $myString, 0, strrpos($myString, ".") ) );
$hell = substr( $myString, strrpos($myString, "."), strlen($myString));
$babe = $txt.$hell;
echo $babe;
$string = 'some_name#somedomain.com';
$res = explode('#', $string);
$ext = '.jpg';
$newString = $res . $ext;
my result turn out to be only .jpg when I expected some_name.jpg
explode returns an array, so you'll need to pick an element:
$newString = $res[0] . $ext;
You need to index into the array you're creating with explode():
$newString = $res[0] . $ext;
$res contains an array, use this:
$newString = $res[0] . $ext;
You should concatenate $res[0] and $ext:
$newString = $res[0] . $ext;
After exploding the string it convert into array :
$res is an array. So try
$newString = $res[0] . $ext;
and you can check this by print_r($res); and use that index which you want.
You're trying to join an array with a string. Choose an element and concatenate.
$string = 'blablabla#gmail.com';
$result = explode('#', $string);
$ext = '.jpg';
$newString = $result[0] . $ext;
$res is an array. You need $res[0] instead.
$string = 'some_name#somedomain.com';
$res = explode('#', $string);
$ext = '.jpg';
$newString = $res[0] . $ext;
$newString = $res[0] . $ext;
I want to replace any string before "/", irrespective of the string length.
Thanks
Jean
one way, assuming you want to change the string before the first "/".
$str = "anystring/the_rest/blah";
$s = explode("/",$str);
$s[0]="new string";
print_r ( implode("/",$s) );
echo preg_replace('/^[^\/]+/', 'baz', 'foo/bar');
Something like that would be the most efficient, although i still prefer the preg_replace() technique
$pos = strpos($input, '/');
if ($pos >= 0) {
$output = $replacement . substr($input, $pos);
} else {
$output = $input;
}