Php, number of occurrences in a string - php

I'm looking for a way to count the number of occurrences in a string, I find 2 php function to do this:
substr_count
count_chars
I try to understand what is the difference between this 2 functions.
The major difference i saw is that substr_count is case sensitive, But i didn't understand about count_chars the following statement:
Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.
If anyone can please explain the major differences between this 2 functions, Maybe some example can help.

count_chars() counts the number of occurrences of every byte value, whereas substr_count() will count the number of occurrences of a particular substring.
substr_count() can be used to search for the number of occurrences of a substring inside a selective region of the string, which is not possible with count_chars().
count_chars() returns an array / string depending on the argument, but substr_count() will always return an integer.

substr_countcounts the occurences of a given string in another string; while count_charscounts the occurences of every single character in the given string

Related

php - count particular permutations

I have some strings that look like this :
431234412
What I am trying to do is find all the instances of '1234' in each string BUT the numbers can be used again so long as they are in order.
So for instance, in the above string there would be 2 instances of '1234' since '12344' can be interpreted as '1234-' or '123-4'.
I was thinking of writing recursive functions but maybe there is simple way to do with regex?
The regex to do that is quite simple:
$subject= '431234412';
preg_match_all('/(1+2+3+4)+/', $subject, $matches);
You will find all occurrences in the array $matches.
The + behind each digit means, that this digit may be repeated.

Regex match number consisting of specific range, and length?

I'm trying to match a number that may consist of [1-4], with a length of {1,1}.
I've tried multiple variations of the following, which won't work:
/^string\-(\d{1,1})[1-4]$/
Any guidelines? Thanks!
You should just use:
/^string-[1-4]$/
Match the start of the string followed by the word "string-", followed by a single number, 1 to 4 and the end of the string. This will match only this string and nothing else.
If this is part of a larger string and all you want is the one part you can use something like:
/string-[1-4]\b/
which matches pretty much the same as above just as part of a larger string.
You can (in either option) also wrap the character class ([1-4]) in parentheses to get that as a separate part of the matches array (when using preg_match/preg_match_all).
This is not hard:
/^string-([1-4]{1})$/

Regular expression to match an exact number of occurrence for a certain character

I'm trying to check if a string has a certain number of occurrence of a character.
Example:
$string = '123~456~789~000';
I want to verify if this string has exactly 3 instances of the character ~.
Is that possible using regular expressions?
Yes
/^[^~]*~[^~]*~[^~]*~[^~]*$/
Explanation:
^ ... $ means the whole string in many regex dialects
[^~]* a string of zero or more non-tilde characters
~ a tilde character
The string can have as many non-tilde characters as necessary, appearing anywhere in the string, but must have exactly three tildes, no more and no less.
As single character is technically a substring, and the task is to count the number of its occurences, I suppose the most efficient approach lies in using a special PHP function - substr_count:
$string = '123~456~789~000';
if (substr_count($string, '~') === 3) {
// string is valid
}
Obviously, this approach won't work if you need to count the number of pattern matches (for example, while you can count the number of '0' in your string with substr_count, you better use preg_match_all to count digits).
Yet for this specific question it should be faster overall, as substr_count is optimized for one specific goal - count substrings - when preg_match_all is more on the universal side. )
I believe this should work for a variable number of characters:
^(?:[^~]*~[^~]*){3}$
The advantage here is that you just replace 3 with however many you want to check.
To make it more efficient, it can be written as
^[^~]*(?:~[^~]*){3}$
This is what you are looking for:
EDIT based on comment below:
<?php
$string = '123~456~789~000';
$total = preg_match_all('/~/', $string);
echo $total; // Shows 3

check if 5 digit number is available in a string

$st1='dsdsdsd 97537 sdsdd dsddd';
$st2='fdsf 23e sdsd 434 432443454';
$st3='fdf97537 ds344dsddd';
I want to check whether a 5 digit number is available in a string.
st1-- has 5 digit number
st2--- not
st3-- has 5 digit number
A simple regex will do the job.
preg_match('/\d{5}/', $input)
See also http://www.php.net/preg_match
Try this regular expression with preg_match() or preg_match_all()
preg_match("/\b[^\d]*\d{5}[^\d]*\b/", $str);
Let's assume that each element to be checked if this is five digits number is separated by space in string. Therefore you may use explode function to convert string into array of substrings. next you can use is_numeric function to check if that is digit along with check if that sub string is five length long. Also you may use regular expression for that.
Here RegEx is far more better. As I see the #Matt's answer meets these requirements, my comments will be unnecessary.

How to find number of occurences of string in comma separated values field

I have a field that contains comma separated values.
I found I can verify the presence in the list of "b" using this code:
SELECT FIND_IN_SET('b','a,b,c,d,b,b')
I want a different thing:
I want to find the number of occurrences of 'b' in that comma separated list. Is this possible in MySQL? Or I must demand it to array_count_values in PHP?
This blog post seems to do what you want:
http://nostylesheets.com/2009/07/17/mysql-substr-count/
Basically, it looks at the string length of the field, removes your target sub-string, then looks at the new length. If you know your substring was 4 characters long, and your new string is now 8 characters shorter than it was, then you know you had 2 occurrences.
LOCATE should do the trick. Note that there are two different signatures - the first (two args) is to be called initially, then call the second (three args), giving the result of the first, as the third argument to the second. More info at:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_locate
LOCATE(substr,str), LOCATE(substr,str,pos)
The first syntax returns the position of the first occurrence of
substring substr in string str. The second syntax returns the position
of the first occurrence of substring substr in string str, starting at
position pos. Returns 0 if substr is not in str.

Categories