Joining two text after explode - php

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

Related

PHP multidimensional array: append indexed multidimensional array based on var

I am using multidimensional arrays and am accessing them after using explode on a string. The resulting array should be nested with the number of occurrences of '.' in the given string. For instance, foo.bar.ok is ['foo']['bar']['ok'].
Currently, I am doing:
switch (count($_match)):
case 1:
$retVal = str_replace('{' . $match . '}', $$varName[$_match[0]], $retVal);
break;
case 2:
$retVal = str_replace('{' . $match . '}', $$varName[$_match[0]][$_match[1]], $retVal);
break;
case 3:
$retVal = str_replace('{' . $match . '}', $$varName[$_match[0]][$_match[1]][$_match[2]], $retVal);
break;
endswitch;
Quintessentially I would like to have unlimited number of $_match[x] using a loop.
Edit
The resulting array should be in the format: $array[foo][bar][ok]
Here are some examples I tried:
$string = 'foo.bar.ok';
$exploded = explode('.', $string);
$bracketed = array_map(function($x) { return [$x]; }, $exploded);
echo "<pre>";var_dump($bracketed);
$bracketed = array_map(function($x) { return "['$x']"; }, $_match);
$result = implode('', $bracketed);
var_dump(eval('return $t' . $result . ';'));
The first doesn't append arrays in nested structure, it lists them as 0, 1, 2, etc and the second works but it uses eval.
Finally, using loops as suggested worked.
for ($replace = $$varName[$_match[0]], $i = 1; $i < count($_match); $i++) {
if (isset($replace[$_match[$i]]))
$replace = $replace[$_match[$i]];
}
if (is_string($replace) || is_numeric($replace))
$retVal = str_replace('{' . $match . '}', $replace, $retVal);
I would like to see a working array_walk example though? - thank you!
Use array_map() and implode().
$string = 'foo.bar.ok';
$exploded = explode('.', $string);
$bracketed = array_map(function($x) { return "[$x]"; }, $exploded);
$result = implode('', $bracketed);
So you want to perform $$varName[$_match[0]][$_match[1]][$_match[2]...[$_match[count($_match)-1]?
for ($var_name = $varName[$_match[0]], $i=1; $i<count($_match); $i++) {
$var_name = $var_name[$_match[$i]];
}
$retVal = str_replace('{' . $match . '}', $$var_name, $retVal);
This could probably be improved by replacing the for() with array_walk().

PHP : Cut Direction from string

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;

making file name lower case while preserving extension

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;

PHP - Convert a string with dashes while removing first word

$title = '228-example-of-the-title'
I need to convert the string to:
Example Of The Title
How would I do that?
A one-liner,
$title = '228-example-of-the-title';
ucwords(implode(' ', array_slice(explode('-', $title), 1)));
This splits the string on dashes (explode(token, input)),
minus the first element (array_slice(array, offset))
joins the resulting set back up with spaces (implode(glue, array)),
and finally capitalises each word (thanks salathe).
$title = '228-example-of-the-title'
$start_pos = strpos($title, '-');
$friendly_title = str_replace('-', ' ', substr($title, $start_pos + 1));
You can do this using the following code
$title = '228-example-of-the-title';
$parts = explode('-',$title);
array_shift($parts);
$title = implode(' ',$parts);
functions used: explode implode and array_shift
$pieces = explode("-", $title);
$result = "";
for ($i = 1; $i < count(pieces); $i++) {
$result = $result . ucFirst($pieces[$i]);
}
$toArray = explode("-",$title);
$cleanArray = array_shift($toArray);
$finalString = implode(' ' , $cleanArray);
// echo ucwords($finalStirng);
Use explode() to split the "-" and put the string in an array
$title_array = explode("-",$title);
$new_string = "";
for($i=1; $i<count($title_array); $i++)
{
$new_string .= $title_array[$i]." ";
}
echo $new_string;

extract part of file name

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

Categories