Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a string such as this:
10,14.5,3.6,10.4,2
I'd like to round down each number so the result would be this:
10,14,3,10,2
I'm looking for the cleanest and fastest solution? My first thought was to explode them into an array and run a loop and floor each value, but that seems a little clunky to me.
I also thought that maybe using regex to just remove everything after a decimal for each value, but I don't know how to do that or if it's more efficient?
Anyone have any suggestions?
$array = explode(",",'10,14.5,3.6,10.4,2');
$string = implode(",",array_map('floor',$array));
Example
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
There are two arrays. It is necessary to display a list from the first array on the page, in which the elements of the second array will be highlighted, for example with a color. Probably it is necessary to read one array in a loop and compare, for example preg_match with the second array and if there is a coincidence - to allocate. Does anyone have a more beautiful solution?
There is an array_intersect function that does pretty much what you need. If you want to compare the elements using regular expressions, try preg_grep, but you will need to prepare the pattern first.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to generate all possible combinations of a string using PHP. I've searched this site for a while trying to find the right algorithm but I can't seem to find the correct one. To be more specific if i input the string "hi", I want it to return "hi", "ih", "h", "i". But I don't want it to reuse the same characters multiple times. So I wouldn't want "hh" and "ii". Is there an algorithm for this? Thanks!
Dont take this for production:
$string = implode('',array_unique(str_split('helpa')));
$i=0;
while($i++<50000){
$coll[substr(str_shuffle($string),0,mt_rand(1,strlen($string)))]=true;
}
ksort($coll);
print '<pre>';
print_r(array_keys($coll));
Here you can test what you want to get.
In 10000 iteration i was getting 325 combinations from a 5 length string
(what seems all possible combinations)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a set of strings that are in the format of This.is.An.Example.YYYY.More.Data.Here where YYYY is any valid year and I would like to get the string up until YYYY in the example so I would end up with a string that is This.is.an.Example.
You can do this with a simple regex, \d{4}.
$splitted = preg_split('~\d{4}~', 'This.is.An.Example.2015.More.Data');
echo $splitted[0];
Output:
This.is.An.Example.
This will split one every 4 continuous single numbers (0-9). Since you only care about what is the first instance use the 0 index.
Sample: http://sandbox.onlinephpfunctions.com/code/640224e9bda3e6cd5c3e012a1bb901dc1e6a34b4
Vague question, so I choose the simplest version ;-)
<?php
$in = 'This.is.An.Example.2015.More.Data.Here';
$out = strstr($in, '.2015', true);
echo $out;
prints This.is.An.Example
see http://docs.php.net/strstr
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Please any one help me. As I am a noobe in PHP coding. Is there any built in function to trim letters in PHP??
Lookup substr()
http://us2.php.net/substr
Should do the trick
$var = 'teststring1234567890';
substr($var, 0, -10);
returns 'teststring'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this long number 2241574459 , how can i convert this to string or readable number ?Is there any php functions ?
I know exactly how to accomplish this common task:
$variable = preg_replace(
"/\d{5,}/",
"string or readable number",
$variable
);
This will replace all 5+ digit numbers with string or readable number.
Moral of the story: Make your question clear -- how can anyone know what that integer represents? This is actually the only valid answer to it, as stupid as it is.
$text="2241574459";
$zahl=(int) $text;
Unless I misunderstand your question.