In php how to replace a '/' and find in between numbers
Example:
$a = 30/36;
// i need to get $a value is 30,31,32,33,34,35,36 using php
print_r(range(...explode('/', '30/36')));
Try This
$a = '30/36';
$explode=(explode("/",$a));
print_r($explode);
$range=(range($explode[0],$explode[1])) ;
echo implode(",",$range);
Simple google search would give you the answer. ..
$a = '1/2/3/4/5';
$res = str_replace('/', ',', $a);
echo $res;
EDIT: As per #NigelRen request! Took part from #u_mulder (i think acceptable answer) to complete my answer!
$a = '30/36';
$res = str_replace('/', ',', $a);
$range = range(...explode(',', '30,36'));
$range = implode(',', $range);
echo $range;
Related
I have problems to get my percent counter function to get it work with English money format
function calc_proc($price, $savings) {
$old_price = $price + $savings;
return (number_format($savings / ($old_price/100)));
}
because of the commas I'm getting bad values
Is english money format something like this: 253,17?
If yes, then simply do:
str_replace(',', '.', $value);
Then you're safe.
First of all you don't have to use strings in financial calculations but if you still have to use, you should replace commas with dots. For example,
$a = '1,5';
$b = '2,1';
echo $a + $b; //result 3
// you can avoid this, by replacing comma:
$a = str_replace(',', '.', $a);
//same for $b
Another solution could be setting locale but you'd have issues with dots after that.
In your function it'll look like this:
function calc_proc($price, $savings) {
$price = str_replace(',', '.', $price);
$savings = str_replace(',', '.', $savings);
$old_price = (float)$price + (float)$savings;
return (number_format($savings / ($old_price/100)));
}
The PHP number_format() function without parameters return english format with comma. You should instead use:
return round($savings / ($old_price/100), 0);
and make sure $old_price cant be 0
function percent($price, $savings) {
$count1 = str_replace(',', '.', $price)/ $savings;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
return $count;
}
Try setlocale(constant, location) at the beginning of your script. In your case:
setlocale(LC_ALL,"En-Us");
I have a string I get from a website.
A portion of the string is "X2" I want to add +1 to 2.
The entire string I get is:
20120815_00_X2
What I want is to add the "X2" +1 until "20120815_00_X13"
You can do :
$string = '20120815_00_X2';
$concat = substr($string, 0, -1);
$num = (integer) substr($string, -1);
$incremented = $concat . ($num + 1);
echo $incremented;
For more informations about substr() see => documentation
You want to find the number at the end of your string and capture it, test for a maximum value of 12 and add one if that's the case, so your pattern would look something like:
/(\d+)$/ // get all digits at the end
and the whole expression:
$new = preg_replace('/(\d+)$/e', "($1 < 13) ? ($1 + 1) : $1", $original);
I have used the e modifier so that the replacement expression will be evaluated as php code.
See the working example at CodePad.
This solution works (no matter what the number after X is):
function myCustomAdd($string)
{
$original = $string;
$new = explode('_',$original);
$a = end($new);
$b = preg_replace("/[^0-9,.]/", "", $a);
$c = $b + 1;
$letters = preg_replace("/[^a-zA-Z,.]/", '', $a);
$d = $new[0].'_'.$new[1].'_'.$letters.$c;
return $d;
}
var_dump(myCustomAdd("20120815_00_X13"));
Output:
string(15) "20120815_00_X14"
I have this string:
467:some-text-here-1786
How can I select only the first numerical value before the ":" ?
Thank you
Very simple:
list($var) = explode(":",$input);
or
$tmp = explode(":",$input);
$var = array_shift($tmp);
or (as pointed out by PhpMyCoder)
$tmp = current(explode(":",$input));
$string = '467:some-text-here-1786';
$var = (int)$string;
Since you're extracting a number, this is enough :)
For an explanation of why this works, check the official PHP manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
It's also really fast and really safe: you are sure you get a number.
$a = "467:some-text-here-1786";
$a = explode(":", $a);
$a = $a[0];
another way to do this:
$length = strpos($string, ':') + 1;
$number = substr($string, 0, $length);
I have this "¶ms=&offer=art-by-jeremy-johnson" stored in my data base.
Is there any function / method to get the output as "Art by Jeremy Johnson" using the above as the input value. this should be changed to the output "Art by Jeremy Johnson" only on the runtime.
can this be done in PHP.
Please help.
$orig = '¶ms=&offer=art-by-jeremy-johnson';
$parts = explode('=', $orig);
$output = explode('-', end($parts));
echo ucwords(implode(' ', $output));
In Java, I guess you can just use lastIndexOf to get the last index of the equals sign, and get the remainder of the string (using substring).
if (myString.lastIndexOf("=") != -1) {
String words = myString.substring(myString.lastIndexOf("=")+1);
words.replaceAll("-", " ");
return words;
}
$string="¶ms=&offer=art-by-jeremy-johnson";
parse_str($string,$output);
//print_r($output);
$str=ucwords(str_replace("-"," ",$output['offer']));
If I understand well you want to not capitalized some words.
Here is a way to do it :
$str = "¶ms=&offer=art-by-jeremy-johnson";
// List of words to NOT capitalized
$keep_lower = array('by');
parse_str($str, $p);
$o = explode('-', $p['offer']);
$r = array();
foreach ($o as $w) {
if (!in_array($w, $keep_lower))
$w = ucfirst($w);
$r[] = $w;
}
$offer = implode(' ', $r);
echo $offer,"\n";
output:
Art by Jeremy Johnson
We are trying to get certain parts of a String.
We have the string:
location:32:DaD+LoC:102AD:Ammount:294
And we would like to put the information in different strings. For example $location=32 and $Dad+Loc=102AD
The values vary per string but it will always have this construction:
location:{number}:DaD+LoC:{code}:Ammount:{number}
So... how do we get those values?
That would produce what you want, but for example $dad+Loc is an invalid variable name in PHP so it wont work the way you want it, better work with an array or an stdClass Object instead of single variables.
$string = "location:32:DaD+LoC:102AD:Ammount:294";
$stringParts = explode(":",$string);
$variableHolder = array();
for($i = 0;$i <= count($stringParts);$i = $i+2){
${$stringParts[$i]} = $stringParts[$i+1];
}
var_dump($location,$DaD+LoC,$Ammount);
Easy fast forward approach:
$string = "location:32:DaD+LoC:102AD:Ammount:294";
$arr = explode(":",$string);
$location= $arr[1];
$DaD_LoC= $arr[3];
$Ammount= $arr[5];
$StringArray = explode ( ":" , $string)
By using preg_split and mapping the resulting array into an associative one.
Like this:
$str = 'location:32:DaD+LoC:102AD:Ammount:294';
$list = preg_split('/:/', $str);
$result = array();
for ($i = 0; $i < sizeof($list); $i = $i+2) {
$result[$array[$i]] = $array[$i+1];
};
print_r($result);
it seems nobody can do it properly
$string = "location:32:DaD+LoC:102AD:Ammount:294";
list(,$location,, $dadloc,,$amount) = explode(':', $string);
the php function split is deprecated so instead of this it is recommended to use preg_split or explode.
very useful in this case is the function list():
list($location, $Dad_Loc, $ammount) = explode(':', $string);
EDIT:
my code has an error:
list(,$location,, $Dad_Loc,, $ammount) = explode(':', $string);