How do I know how many arguments explode created - php

Using explode(), How can I check how many arguments explode created? Is there function which check this or do I have to primary check how many times a character I chose to split on appears in string?

explode() return an array, the number of array elements can be returned with count().
$number = count(explode([a, b, c])); // 3

Return array after explode, use count() will do.
$str = 'Apple, Mango, Orange, Banana';
$exp = explode(',',$str);
echo count($exp);

Related

how to input elements instead of another array, into end of array?

Due to bad DB design, there may be several values in a column # each row in a table. So I had to take in every string, check if commas exist (multiple values) & place each element into the end of an array.
Did try out functions like strpos, explode, array_push etc. With the folllowing code, how do i input ONLY the multiple elements into the end of an array, without creating another & placing that into an existing array?
$test = array();
$test = array ("testing");
$str = 'a,b,c,d';
$parts = explode(',', $str);
array_push ($test, $parts); //another array inserted into $test, which is not what I want
print_r($test);
Use array_merge.
$test = array_merge($test, $parts);
Example: http://3v4l.org/r7vaB

How to use explode and get first element in one line in PHP?

$beforeDot = explode(".", $string)[0];
This is what I'm attempting to do, except that it returns syntax error. If there is a workaround for a one liner, please let me know. If this is not possible, please explain.
The function array dereferencing was implemented in PHP 5.4, so if you are using an older version you'll have to do it another way.
Here's a simple way to do it:
$beforeDot = array_shift(explode('.', $string));
You can use list for this:
list($first) = explode(".", "foo.bar");
echo $first; // foo
This also works if you need the second (or third, etc.) element:
list($_, $second) = explode(".", "foo.bar");
echo $second; // bar
But that can get pretty clumsy.
Use current(), to get first position after explode:
$beforeDot = current(explode(".", $string));
Use array_shift() for this purpose :
$beforeDot = array_shift(explode(".", $string));
in php <= 5.3 you need to use
$beforeDot = explode(".", $string);
$beforeDot = $beforeDot[0];
2020 : Google brought me here for something similar.
Pairing 'explode' with 'implode' to populate a variable.
explode -> break the string into an array at the separator
implode -> get a string from that first array element into a variable
$str = "ABC.66778899";
$first = implode(explode('.', $str, -1));
Will give you 'ABC' as a string.
Adjust the limit argument in explode as per your string characteristics.
You can use the limit parameter in the explode function
explode($separator, $str, $limit)
$txt = 'the quick brown fox';
$explode = explode(' ', $txt, -substr_count($txt, ' '));
This will return an array with only one index that has the first word which is "the"
PHP Explode docs
Explanation:
If the limit parameter is negative, all components except the last
-limit are returned.
So to get only the first element despite the number of occurences of the substr you use -substr_count

A more forgiving array_intersect in PHP

array_intersect takes two arrays and looks for matching === values and returns the result. However the values in the array have to match character for character. Is there a function or a method for comparing two arrays and looking for values that contain similar strings instead of equal similar strings. Something like stripos but with array_intersect.
$array1 = array("howdyhorse", "monkeyjoe", "bill", "donkeymonkey", "carrothorse")
$array2 = array("bill", "horse", "monkeybunk", "apple", "panda")
function($array1, $array2);
Returns an array = array("bill", "horse", "monkeyjoe")
The order is of no particular concern.
Is running all the values of each array through something like
foreach( $array as $slice )
$slice = trim( preg_replace( $pattern, $replacement ) ) ;
to make everything lowercase and remove spaces and special chars and then doing an array_intersect an option?
You could use array_uintersect and similar_text. similar_text is O(N**3), so you need to write your own function if your compare similar logic is simpler.

How to change numbers to random positions in PHP

How would I go about changing the following numbers around to a random positions in php?
Do I need to explode the numbers?
40,52,78,81,25,83,37,77
Thanks
$arr = explode(',', '40,52,78,81,25,83,37,77');
shuffle($arr);
echo implode(',', $arr);
http://ideone.com/sh2uH
So you want to shuffle the array order? Use PHP's shuffle function.
http://php.net/manual/en/function.shuffle.php
EDIT:
Didn't realise your numbers were in a string. The other answer sums it up.
Assuming the numbers are in a string:
$numbers = '40,52,78,81,25,83,37,77';
$numbers = explode(',',$numbers);
shuffle($numbers);
$numbers = implode(',',$numbers);
Try something like this.
$string = "40,52,78,81,25,83,37,77";
$numbers = explode(",", $string);
shuffle($numbers);
print_r($numbers);
explode breaks the string out into an array separating entries by ,
shuffle will operate on the array by reference and put them in random order

substr_count and an array as a needle

How to use substr_count with an array as a needle. Like this:
substr_count($str, array('find_this', 'or_find_this'));
You could use implode() to create a string of the array and create a regex kind of thing.
$array = array('find_this', 'or_find_this');
$string = implode('|', $array);
$count = count(preg_grep("/($string)/", $str));
echo $count;
You'll need to loop through them and add the substr_count() to the total count. A ready example is in the php manual page,
http://www.php.net/manual/en/function.substr-count.php#74952

Categories