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
Related
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);
$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
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
Array {
[0] => http://abc.com/video/ghgh23;
[1] => http://smtech.com/file/mwerq2;
}
I want to replace the content between /sometext/ from the above array. Like I want to replace video, file with abc.
You don't need to loop over every element of the array, str_replace can take an array to replace with:
$myArray = str_replace(array('/video/', '/file/'), '/abc/', $myArray);
However, based on your question, you might want to replace the first path segment, and not a specific index. So to do that:
$myArray = preg_replace('((?<!/)/([^/]+)/)', '/abc/', $myArray);
That will replace the first path element of every URL in $myArray with /abc/...
One way is to use str_replace()
You can check it out here: http://php.net/str_replace
Either str_replace as other comments suggested or using a regular expression especially if you might have a longer url with more segments like http://example.com/xxx/somestuff/morestuff
In that case str_replace will not be enough you will need preg_replace
This is another option. Supply a array, foreach will pick it up and then the first parameter of str_replace can be a array if needed. Hope you find this helpful.
<?php
$array = array('http://abc.com/video/ghgh23','http://smtech.com/file/mwerq2');
$newarray = array();
foreach($array as $url) {
$newarray[] = str_replace(array('video','file'),'abc',$url);
}
print_r($newarray);
?>
//every element in $myArray
for($i=0; $i < count($myArray); $i++){
$myArray[$i] = str_replace('/video/','/abc/',$myArray[$i]);
}
$array = array('http://abc.com/video/ghgh23', 'http://smtech.com/file/mwerq2');
foreach ($array as &$string)
{
$string = str_replace('video', 'abc', $string);
$string = str_replace('file', 'abc', $string);
}
Which is the correct way of making strlen return the lenght of several strings put together.
For example if one string is hello and other is jim it should return: 8. (hello=5 jim=3)
I need to get the combined lenght of $array[0] $array[1] $array[2] $array[3] and $array[4]
Thanks
Use implode on it before:
echo strlen(implode($array));
You can also combine it with array_slice if you don't want the whole array:
echo strlen(implode(array_slice($array, 0, 4)));
array_sum(array_map("strlen", $array_of_strings))
$len = 0;
foreach($array as $str)
$len += strlen($str);
use something like this:
$string = '';
foreach ($array as $val) {
$string .= $val;
}
echo strlen($string);
This will avoid multiple strlen calls and hence, should be a bit faster then calling strlen inside the foreach loop, atleast theoretically.
You can use implode() to transform your array of strings in a single string and then strlen the result.
echo strlen(implode('', $array))
If you want to get the collective length of all of the strings in an array, you can do this:
$len = strlen(implode('',$myArray));
*Note that this code will join the strings together with no spaces between them. You can change the first parameter to implode() if you want this to be different.