How to convert this 19,500 string to number. If I do this
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
?>
But I don't think this is correct way.
LIVE DEMO
You can replace the string ',' to '' and then convert into integer by int
<?php
$number = "19,500";
$updatedNumber = str_replace(',','',$number);
echo (int)$updatedNumber ;
NOTE: int is better to use than intval function.
It reduces overhead of calling the function in terms of Speed.
http://objectmix.com/php/493962-intval-vs-int.html
Try this:
<?php
$val = "19,500";
$val = str_replace(',', '.', $val);
$number = (float)$val;
?>
UPDATED:
if comma comes out as a thousands-separator then:
<?php
$val = "19,500";
$val = str_replace(',', '', $val);
$number = (int)$val;
?>
you can simply replace ',' with ''
$newnumber = str_replace(',', '', $val);
the best way is:
<?php
$val = "19,500";
$tmp = str_replace(',', '', $val);
$newnumber = (float)$tmp;
?>
I think you just want to remove the ,:
<?php
$val = "19,500";
$val = strtr($val, array(',' => ''));
$number = intval($val);
var_dump($number);
use strtr() instead of str_replace() in case of multiple ,
When I see your code it's just sting string(5) "19500"
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
var_dump($newnumber);
?>
so you can convert to integer like the following
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
$realinteger = (int)($newnumber);
var_dump($realinteger);
?>
So the result will be int(19500)
Just try this code
$val = "19,500";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val))); // output : 19500
$val = "19,500.25";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val))); // output : 1950025
you can edit delimiter that want to replace with balnk string :)
Or you can try this
$newnumber = preg_match("/[^0-9,. -]/", $val) ? 0 : preg_replace("/[^0-9.-]/", "",$val);
just try one. simple as that :)
<?php
$val = "19,500";
$val = str_replace(',','', $val);
$newnumber = number_format($val,2);
?>
OUTPUT:
19,500.00
If you want to have no decimal you can change the 2 in 0. Like this.
OUTPUT:
19,500
Related
http://www.mywebsite/product-tag/animal/
How to I echo animal without the backslash and in big caps like this:
ANIMAL
$link = 'http://www.mywebsite/product-tag/animal/';
$parts = explode( '/', $link );
echo(strtoupper($parts[4]));
If you want auto searching, then you need to use preg_match.
<?php
$string = 'http://www.mywebsite/product-tag/animal/';
$urlparts = explode("/", $string);
$animal = ($string[strlen($string)-1] == '/'? $urlparts[count($urlparts)-2] : end($urlparts));
echo strtoupper($animal);
?>
actually I just figured it out
$r = $_SERVER['REQUEST_URI'];
$r = explode('/', $r);
$r = array_filter($r);
$r = array_merge($r, array());
$endofurl = $r[1];
echo $endofurl;
I got this
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>'
I want to remove only what comes after <p>text3</p>
My result would be:
$description = '<p>text1</p><p>text2</p><p>text3</p>'
My guess is that we need to use preg_replace with some regex but I can't manage to write a working one.
You could...
function str_occurance($needle, $haystack, $occurance) {
$occurance += 2;
$arr = explode($needle, $haystack, $occurance);
unset($arr[0]);
$arr = array_values($arr);
$key = count($arr) - 1;
unset($arr[$key]);
$str = $needle . implode($needle, $arr);
return $str;
}
Not the prettiest, but it works.
Edit: To use:
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>';
$split = '<p>';
$return = 3;
$new = str_occurance($needle, $description, $return);
echo $new; // returns <p>text1</p><p>text2</p><p>text3</p>
$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;
i have an array contain value like :
Check_Value ('level_codes', '1000', '1001','(1000,1002,1004)', 'DO ', '1')==1
i want to get values and store into an other parametrs like :
$codes = level_codes;
$first_value = 1000;
$second_value = 1001;
$list_values = (1000,1002,1004);
$action = DO;
$timer = 1;
$case = ==;
$status = 1;
is there any one please help me to do this work...!
Use the list() function: http://php.net/list
If the order is always the same, the following will reassign the values to the variables you want:
$parts = preg_split("/'?\(|\)'?/", $original_values);
$sub1 = explode(',', $parts[1]);
$codes = trim(str_replace("'", '', $sub1[0])) ;
$first_value = trim(str_replace("'", '', $sub1[1]));
$second_value = trim(str_replace("'", '', $sub1[2]));
$list_values = "($parts[2])";
$sub2 = explode(',', $parts[3]);
$action = trim(str_replace("'", '', $sub2[1]));
$timer = trim(str_replace("'", '', $sub2[2]));
preg_match('/(\D+)(\d+)/', $parts[4], $matches);
$case = $matches[1];
$status = $matches[2];
There might be a more elegant way to do it, but the original value is a string, rather than an array.
If the array is string, i think you should look into preg_match
i have string like this
$string = 'aaaaaa, bbbbbb, cccccc, ';
and i want to modified it to be like this
$string = 'aaaaaa, bbbbbb, cccccc';
the last ',' and space is removed.
how to do this in php?
what is the function needed the achieve that?
my full code is like this
if(isset($_POST['accomodation'])) $accomodation = 'Accomodation, ';
if(isset($_POST['dance'])) $dance = 'Dance Lessons, ';
if(isset($_POST['vacation'])) $vacation = 'Vacation planning, ';
if(isset($_POST['group'])) $group = 'Group Vacation, ';
if(isset($_POST['inprivate'])) $inprivate = 'Private Vacation, ';
if(isset($_POST['land'])) $land = 'Land purchase/lease';
if(isset($_POST['all'])) $all = 'All';
#$interest = $accomodation.$dance.$vacation.$group.$inprivate.$land;
#echo $string;
*sorry for such dumb question, it's been so long i didn't touch native PHP programming
rtrim() function:
rtrim($string,', ');
but how are you defining the string? It may be that you can build it without the comma and space.
EDIT
$interests = array();
if(isset($_POST['accomodation'])) $interests[] = 'Accomodation';
if(isset($_POST['dance'])) $interests[] = 'Dance Lessons';
if(isset($_POST['vacation'])) $interests[] = 'Vacation planning';
if(isset($_POST['group'])) $interests[] = 'Group Vacation';
if(isset($_POST['inprivate'])) $interests[] = 'Private Vacation';
if(isset($_POST['land'])) $interests[] = 'Land purchase/lease';
if(isset($_POST['all'])) $all = 'All';
$interest = implode(', ',$interests);
echo $interest;
$string = preg_replace('/\s*,\s*$/', '', $string);
or, way cooler:
$string = rtrim($string, " ,");
Note that it does not matter the order of the characters in the pattern string.
#You last update.
This changes some things. You could put all your variables in one array and then implode it. Like so:
$items = array();
$items[] = $accomodation = 'Accomodation';
$items[] = $dance = 'Accomodation';
...
$result = implode(', ', $items)
$string = preg_replace( "/,\s*$/","",$string);
Should do the trick
Is it always a comma then a space at the end?
substr($string, 0, -2)
Often times, you can avoid the trailing comma altogether by changing the way you build the string. For example:
$count = 0;
foreach ($this as $that) {
if ($count != 0) {
$string .= ',';
}
$string .= $that['stuff'];
$count++;
}
Would remove the possibility of any trailing comma at the end, no matter the combination of results.