This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 7 years ago.
I have these two pieces of text
120 - 140 (cm)
and
110 (cm)
I'd like to store the values into an array like
$array[0] = 120
$array[1] = 140
$array_2[0] = 110
How would i do this?
You could do
function numArray($str)
{
$str = preg_replace("/[^0-9,.]/", " ", $str);
return preg_split('/ /', $str, -1, PREG_SPLIT_NO_EMPTY);
}
$str = "120 - 140 (cm)";
$array = numArray($str);
That will return an array with only numbers in it
Something like this?
<?php
$array = array();
$array_2 = array();
$array[0] = 120;
$array[1] = 140;
$array_2[0] = 110;
?>
Related
This question already has answers here:
Read the longest string from an array in PHP 5.3
(3 answers)
Closed 2 years ago.
$array =array("AB","ABC","ABCD","ABCDE","BD");
Requirement: find the longest element in the array
Output:ABCDE
$array =array("AB","ABC","ABCD","ABCDE","BD");
$longstring = $array[0];
foreach( $array as $string ) {
if ( strlen( $string ) > strlen( $longstring ) ) {
$longstring = $string;
}
}
echo $longstring;
First we are iterate the array with help of foreach loop. in foreach loop we check $result is less than array value. if array value is greater then $result array then we override previous value of $result with new value. if we found greatest length than previous array element then we are storing new length & key of new element in variable.
$array =array("AB","ABC","ABCD","ABCDE","BD");
$result = $resultkey = 0;
foreach($array as $key=>$value) {
if($result < strlen($value) ) {
$result = strlen($value);
$resultkey = $key;
}
}
echo 'longest value:' $result;
echo 'result :' $array[$resultkey]
Output:
longest value: 5
result :ABCDE
You could sort the array by string length using strlen and usort and get the first item:
$array =array("AB","ABC","ABCD","ABCDE","BD");
usort($array, function($x, $y) { return strlen($y)-strlen($x); });
echo $array[0];
Result:
ABCDE
Demo
Try this, though it's a bit confusing.
<?php //php 7.0.8
$array = array("AB","ABC","ABCD","ABCDE","BD");
$longertext = array_search(max($array), $array)-1; //4-1 =3
// minus 1 because it's counting the actual position not starts with 0 it returns 4
echo $longertext; //equals 3
echo "\n";
echo $array[$longertext]; //equals ABCDE
?>
The actual test: http://rextester.com/OTI23127
This question already has answers here:
how to do a sum on a string in php?
(5 answers)
Closed 8 months ago.
I have a PHP string that contains numbers. Is there a way to add up all the numbers in a string?
For example: I have a string called $soc_count. That string outputs the following:
1 1
Is there a way to add up all the numbers in a string?
My code:
<?php $soc_count = [];$soc_count = (get_row_layout() == 'irl_today_social_media');?>
<?php echo $soc_count;?>
Assuming numbers are positive integers and string is not empty, you can try this:
eval('$sum = ' . trim(preg_replace('/[^0-9]+/', "+0+", $soc_count), '+') . ';');
echo $sum;
Use explode on your string of numbers, and then sum up the array:
$soc_count = "1 1";
$nums = explode(" ", $soc_count);
$total = array_sum($nums);
echo $total;
For a more general solution, which would cover a string of any type having numbers in it, we can try using preg_match_all:
$soc_count = "1 quick brown fox jumped over 1 lazy dog.";
preg_match_all("/\d+/", $soc_count, $matches);
$nums = $matches[0];
$total = array_sum($nums);
echo $total;
This question already has answers here:
Read the longest string from an array in PHP 5.3
(3 answers)
Closed 2 years ago.
$array =array("AB","ABC","ABCD","ABCDE","BD");
Requirement: find the longest element in the array
Output:ABCDE
$array =array("AB","ABC","ABCD","ABCDE","BD");
$longstring = $array[0];
foreach( $array as $string ) {
if ( strlen( $string ) > strlen( $longstring ) ) {
$longstring = $string;
}
}
echo $longstring;
First we are iterate the array with help of foreach loop. in foreach loop we check $result is less than array value. if array value is greater then $result array then we override previous value of $result with new value. if we found greatest length than previous array element then we are storing new length & key of new element in variable.
$array =array("AB","ABC","ABCD","ABCDE","BD");
$result = $resultkey = 0;
foreach($array as $key=>$value) {
if($result < strlen($value) ) {
$result = strlen($value);
$resultkey = $key;
}
}
echo 'longest value:' $result;
echo 'result :' $array[$resultkey]
Output:
longest value: 5
result :ABCDE
You could sort the array by string length using strlen and usort and get the first item:
$array =array("AB","ABC","ABCD","ABCDE","BD");
usort($array, function($x, $y) { return strlen($y)-strlen($x); });
echo $array[0];
Result:
ABCDE
Demo
Try this, though it's a bit confusing.
<?php //php 7.0.8
$array = array("AB","ABC","ABCD","ABCDE","BD");
$longertext = array_search(max($array), $array)-1; //4-1 =3
// minus 1 because it's counting the actual position not starts with 0 it returns 4
echo $longertext; //equals 3
echo "\n";
echo $array[$longertext]; //equals ABCDE
?>
The actual test: http://rextester.com/OTI23127
This question already has answers here:
explode string into variables
(2 answers)
Closed 5 years ago.
I need to use php and extract the items from the string and assign them to variables.
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
How can I get:
$var1 = 76305203
$var2 = 124400884
To create variables use list()
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
list($var1,$var2) = explode(';', $string);
echo $var1;
echo PHP_EOL;
echo $var2;
Output:- https://eval.in/928536
Or use explode() to get array and use that array
<?php
$string = "76305203 ;124400884 ;109263187 ;current ;18.44";
$array = explode(';', $string);
echo $array[0];
echo PHP_EOL;
echo $array[1];
Output:-https://eval.in/928537
This question already has answers here:
Deleting an element from an array in PHP
(25 answers)
Closed 9 years ago.
How can I delete the 3 in "1,2,3,4"?
Dirty hack: Slice the sliced arrays and join them again.
$s = "1,2,3,4";
$array = explode(',', $s);
$a = array_splice($array, 0, -2);
$b = array_splice($array, 1, 1);
$s = implode(',', $a).','.implode($b);
echo $s; // 1,2,4
Test: http://codepad.org/WeDLKvEE
OK. How about using strpos and instr? Find the second comma and the third one.
$s = "1,2,3,4";
$array = explode(',', $s);
unset($array[2]);
$Arr = array_values($array);
print_r($Arr);
demo : https://eval.in/85585
How about this, by this you do not depend on the position of the string rather can wipe out by value.
$s = "1,2,3,4";
$array = explode(",",$s);
$val = 3 ;
$key = array_search($val,$array);
if($key!==false){
unset($array[$key]);
}
echo implode(",",$array); // 1,2,4