If I have a string like this: '(1.23123, 4.123123)'
How would I convert it to two doubles?
$items = explode(',', $string);
$n1 = $items[0] // (1.23123
My attempts:
floatval($n1) // 0
(double) $n1 // 0
How can I convert?
You need to trim those parenthesis around your string . Use trim inside your explode by passing the parentheses as the charlist.
$items = explode(',', trim($str,')('));
The code
<?php
$str='(1.23123, 4.123123)';
$items = explode(',', trim($str,')('));
$items=array_map('floatval',$items);
echo $n1 = $items[0]; // "prints" 1.23123
echo $n2 = $items[1]; // "prints" 4.123123
array_map('floatval', $array) for your array
Try this code
$string = "(1.23123, 4.123123)";
preg_match_all("/[\d\.]+/", $string, $matches);
print_r($matches[0]);
Related
i know that its easy to extract string between two slashes using explode() function in php, What if the string is like
localhost/used_cars/search/mk_honda/md_city/mk_toyota
i want to extract string after mk_ and till the slashes like:** honda,toyota **
any help would be highly appreciated.
I am doing like this
echo strpos(uri_string(),'mk') !== false ? $arr = explode("/", $string, 2);$first = $arr[0]; : '';
but not working because if user enter mk_honda in any position then explode() is failed to handle that.
Use regex:
http://ideone.com/DNHXsf
<?php
$input = 'localhost/used_cars/search/mk_honda/md_city/mk_toyota';
preg_match_all('#/mk_([^/]*)#', $input, $matches);
print_r($matches[1]);
?>
Output:
Array
(
[0] => honda
[1] => toyota
)
Explode your string by /, then check every element of array with strpos:
$string = 'localhost/used_cars/search/mk_honda/md_city/mk_toyota';
$parts = explode('/', $string);
$r = [];
foreach ($parts as $p) {
// use `===` as you need `mk_` in position 0
if (strpos($p, 'mk_') === 0) {
// 3 is a length of `mk_`
$r[] = substr($p, 3);
}
}
echo'<pre>',print_r($r),'</pre>';
Just try this
$str='localhost/used_cars/search/mk_honda/md_city/mk_toyota';
$str=explode('/',$str);
$final=[];
foreach ($str as $words){
(!empty(explode('_',$words)))?(isset(explode('_',$words)[1]))?$final[]=explode('_',$words)[1]:false:false;
}
$final=implode(',',$final);
echo $final;
It give output as
cars,honda,city,toyota
I would like to get all the strings within a specific reference. example:
$string = '<abc>A1</abc><bcd>B1</bcd><abc>A2</abc><bcd>B2</bcd><abc>A3</abc>';
I would like to get all the elements inside Tags <abc>, </ abc>, listing for example A1 A2 A3.
I tried to use explode like this:
$string = '<abc>A1</abc><bcd>B1</bcd><abc>A2</abc><bcd>B2</bcd><abc>A3</abc>';
$take = explode('<abc>', $string);
foreach ($take as $value) {
$take = explode('</abc>',$value);
It returned: array array array array
You can use a regular expression
$string = '<abc>A1</abc><bcd>B1</bcd><abc>A2</abc><bcd>B2</bcd><abc>A3</abc>';
preg_match_all('/<abc>(.*?)<\/abc>/s', $string, $matches);
print_r($matches[1]);
explode function returns an array. Try this code.
$string = '<abc>A1</abc><bcd>B1</bcd><abc>A2</abc><bcd>B2</bcd><abc>A3</abc>';
$take = explode('<abc>', $string);
foreach ($take as $value) {
$take = explode('</abc>',$value);
echo "<pre>";
print_r($take);
}
How should i add the commas before,in between and after the array in php
here is my code
$arr = array(1,2,3,4,5,6,7,8,9);
$string = implode(',', $arr);
echo $string;
getting output: 1,2,3,4,5,6,7,8,9
expected output : ,1,2,3,4,5,6,7,8,9,
Just concatenate those commas
$string = ','.implode(',', $arr).',';
Another way
$arr = array(1,2,3,4,5,6,7,8,9);
$str = '';
foreach($arr as $ar){
$str .= "-{$ar}";
};
echo $str.'-';
How would I extract the number 33 before the underscore in the following?
33_restoffilename.txt.
would something like following work?
int strPos = strpos("33_filename.txt", "_");
str num = substr ("33_filename.txt" , 0 , strPos );
there are may way to achive this:
Method 1:
$strPos = strpos("33_filename.txt", "_");
echo $num = substr("33_filename.txt" , 0 , $strPos );
Method 2:
$str = '33_filename.txt';
$str_arr = explode('_', $str);
echo $num = $str_arr[0];
If the naming convention is always number_filename.ext, you can use explode():
//Put the filename into the variable $name
$name = "33_restoffilename.txt";
//Split the name by "_"
$parts = explode("_", $name);
//Get the first part of the name from the array (position 1)
$number = $parts[0];
//Output
echo $number;
This will output
33
Use strstr().
$str = '33_filename.txt';
$num = strstr($str, '_', true);
Method - 1
$getIntegerFromBeginning = substr($string, 0, strspn($string, "0123456789"));
echo $getIntegerFromBeginning;
Method - 2
$getIntegerFromBeginning = explode("_", $string, 2);
echo $getIntegerFromBeginning[0];
Replace $string with exact string.
Use this
$string = '33_filename.txt';
$arrString = explode('_', $string);
echo $value = $arrString[0]; //Will output 33
This is really very simple we don't need these string functions in this case. Just do type casting and you will get integer number. It will be fast and easy
$pp='33_restoffilename.txt';
echo (int)$pp;
No need to make it complicated
I have done a lot of search. Almost every answer is about array. In my situation, I want to remove the same number.
<?php
$term="1,2,3.4";
$n='2';
//I want to remove 2 when the $n equal one number of $term.
// echo out like 1,3,4
?>
This should work for you:
(I assume that 1,2,3.4 the dot only was a typo)
<?php
$term = "1,2,3,4";
$n = "2";
$arr = explode(",", $term);
if(($key = array_search($n, $arr)) !== FALSE)
//^^^ to make sure when '$n' is not found in the array, that it doesn't unset the first array element
unset($arr[$key]);
echo implode(",", $arr);
?>
Output:
1,3,4
I have done a lot of search.
And you didn't find str_replace() function?
$string = '1,2,3,4,5,6;'
$n = '2';
$string = str_replace($n, '', $string);
$string = str_replace(',,', ',', $string);
No need to waste memory with arrays or using regular expressions.
$term = "1,2,3,4";
$n = 2;
$term_array = explode(',', $term);
$n_key = array_search($n, $term_array);
if ($n_key !== false)
unset($term_array[$n_key]);
$new_terms = implode(',', $term_array);
Ouput :
1,3,4
Hope this helps
$n = '2';
$str = '2,1,2,3,4,5,6,2';
$pattern = '/,2|,2,|2,/';
$after = preg_replace($pattern, '', $str);
echo $after
Ouput
1,3,4,5,6
mybe it's more simple