php посчитать количество вхождений подстроки в строку

substr_count

(PHP 4, PHP 5, PHP 7, PHP 8)

substr_count — Возвращает число вхождений подстроки

Описание

Эта функция не подсчитывает перекрывающиеся подстроки. Смотрите пример ниже!

Список параметров

Строка, в которой ведётся поиск

Смещение начала отсчёта. Если задано отрицательное значение, отсчёт позиции будет произведён с конца строки.

Возвращаемые значения

Эта функция возвращает целое число ( int ).

Список изменений

Примеры

Пример #1 Пример использования substr_count()

Смотрите также

User Contributed Notes 10 notes

500KB string on our web server. It found 6 occurrences of the needle I was looking for in 0.0000 seconds. Yes, it ran faster than microtime() could measure.

Looking to give it a challenge, I then ran it on a Mac laptop from 2010 against a 120.5MB string. For one test needle, it found 2385 occurrences in 0.0266 seconds. Another test needs found 290 occurrences in 0.114 seconds.

Long story short, if you’re wondering whether this function is slowing down your script, the answer is probably not.

Making this case insensitive is easy for anyone who needs this. Simply convert the haystack and the needle to the same case (upper or lower).

To account for the case that jrhodes has pointed out, we can change the line to:

array (
0 => «mystringth»,
1 => «atislong»
);

It was suggested to use

instead of the function described previously, however this has one flaw. For example this array:

array (
0 => «mystringth»,
1 => «atislong»
);

If you are counting «that», the implode version will return 1, but the function previously described will return 0.

Yet another reference to the «cgcgcgcgcgcgc» example posted by «chris at pecoraro dot net»:

Your request can be fulfilled with the Perl compatible regular expressions and their lookahead and lookbehind features.

This will handle a string where it is unknown if comma or period are used as thousand or decimal separator. Only exception where this leads to a conflict is when there is only a single comma or period and 3 possible decimals (123.456 or 123,456). An optional parameter is passed to handle this case (assume thousands, assume decimal, decimal when period, decimal when comma). It assumes an input string in any of the formats listed below.

below was suggested a function for substr_count’ing an array, yet for a simpler procedure, use the following:

Unicode example with «case-sensitive» option;

In regards to anyone thinking of using code contributed by zmindster at gmail dot com

Please take careful consideration of possible edge cases with that regex, in example:

This would cause a infinite loop and for example be a possible entry point for a denial of service attack. A correct fix would require additional code, a quick hack would be just adding a additional check, without clarity or performance in mind:

Источник

count_chars

(PHP 4, PHP 5, PHP 7, PHP 8)

count_chars — Возвращает информацию о символах, входящих в строку

Описание

Подсчитывает количество вхождений каждого из символов с ASCII-кодами в диапазоне (0..255) в строке string и возвращает эту информацию в различных форматах.

Список параметров

Смотрите возвращаемые значения.

Возвращаемые значения

Список изменений

ВерсияОписание
8.0.0До этой версии функция возвращала false в случае возникновения ошибки.

Примеры

Пример #1 Пример использования count_chars()

Результат выполнения данного примера:

Смотрите также

User Contributed Notes 11 notes

If you have problems using count_chars with a multibyte string, you can change the page encoding. Alternatively, you can also use this mb_count_chars version of the function. Basically it is mode «1» of the original function.

count_chars for multibyte supported.

// Require (n) unique characters in a string
// Modification of a function below which ads some flexibility in how many unique characters are required in a given string.

$pass = ‘123456’ ; // true
$pass = ‘111222’ ; // false

I have no idea where this could be used, but it’s quite fun

This function is great for input validation. I frequently need to check that all characters in a string are 7-bit ASCII (and not null). This is the fastest function I have found yet:

Here’s a function to count number of strings in a string. It can be used as a simple utf8-enabled count_chars (but limited to a single mode).

Another approach to counting unicode chars.

Checking that two strings are anagram:

// Usefulness of the two functions

Here are some more experiments on this relatively new and extremely handy function.

= ‘I have never seen ANYTHING like that before! My number is «4670-9394».’ ;

#The result looks like
#The character » » has appeared in this string 11 times.

#This shows that ’70 is not the same as 36′
?>

As we can see above:

1)If you cares only about what is in the string, use count_chars($string, 1) and it will return an (associative?) array of what shows up only.

2) Either I misunderstood what the manul actually said, or it does not work the way it described: count_chars($strting, 3) actually returned a string of what characters are in the string, not a string of their byte-values (which is great because a string of numbers would be much harder to handle);

3)This is a short version of password checking: get the original string’s length, then compare with the length of the string returned by count_chars($string,3).

4) Final trick: now we have a primitive way to count the number of words in a string! (or do we have a fuction for that already?)

this code can find each characters count

Источник

str_word_count

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

str_word_count — Возвращает информацию о словах, входящих в строку

Описание

Для этой функции «слово» обозначает строку с алфавитными символами, зависящую от локали, которая также может содержать символы «‘» и «-«, но не может начинаться с них. Обратите внимание, что многобайтовые языковые стандарты не поддерживаются.

Список параметров

Список дополнительных символов, которые будут рассматриваться как «слово»

Возвращаемые значения

Список изменений

Примеры

Пример #1 Пример использования str_word_count()

= «Hello fri3nd, you’re
looking good today!» ;

Результат выполнения данного примера:

Смотрите также

User Contributed Notes 32 notes

/***
* This simple utf-8 word count function (it only counts)
* is a bit faster then the one with preg_match_all
* about 10x slower then the built-in str_word_count
*
* If you need the hyphen or other code points as word-characters
* just put them into the [brackets] like [^\p\p\’\-]
* If the pattern contains utf-8, utf8_encode() the pattern,
* as it is expected to be valid utf-8 (using the u modifier).
**/

We can also specify a range of values for charlist.

Array ( [0] => Hello [1] => fri3nd [2] => you’re [3] => looking [4] => good [5] => today [6] => look123 [7] => ing )

Here is a count words function which supports UTF-8 and Hebrew. I tried other functions but they don’t work. Notice that in Hebrew, ‘»‘ and ‘\» can be used in words, so they are not separators. This function is not perfect, I would prefer a function we are using in JavaScript which considers all characters except [a-zA-Zא-ת0-9_\’\»] as separators, but I don’t know how to do it in PHP.

I removed some of the separators which don’t work well with Hebrew («\x20», «\xA0», «\x0A», «\x0D», «\x09», «\x0B», «\x2E»). I also removed the underline.

Hi this is the first time I have posted on the php manual, I hope some of you will like this little function I wrote.

It returns a string with a certain character limit, but still retaining whole words.
It breaks out of the foreach loop once it has found a string short enough to display, and the character list can be edited.

This function doesn’t handle accents, even in a locale with accent.
echo str_word_count ( «Is working» ); // =2

//To get an accurate word count in English, some diacritical marks have
// to be added for words like née, Chloë, naïve, coöpt, façade, piñata, etc.
$count = str_word_count($str, 0, ‘éëïöçñÉËÏÖÇÑ’);

//To get the word count for any European language using a Roman alphabet:
$count = str_word_count($str, 0, ‘äëïöüÄËÏÖÜáǽćéíĺńóŕśúźÁǼĆÉÍĹŃÓŔŚÚŹ’.
‘àèìòùÀÈÌÒÙãẽĩõñũÃẼĨÕÑŨâêîôûÂÊÎÔÛăĕğĭŏœ̆ŭĂĔĞĬŎŒ̆Ŭ’.
‘āēīōūĀĒĪŌŪőűŐŰąęįųĄĘĮŲåůÅŮæÆøØýÝÿŸþÞẞßđĐıIœŒ’.
‘čďěľňřšťžČĎĚĽŇŘŠŤŽƒƑðÐłŁçģķļșțÇĢĶĻȘȚħĦċėġżĊĖĠŻʒƷǯǮŋŊŧŦ’);

For spanish speakers a valid character map may be:

preg_match_all based function to mimic str_word_count behavior:

This example may not be pretty, but It proves accurate:

I needed a function which would extract the first hundred words out of a given input while retaining all markup such as line breaks, double spaces and the like. Most of the regexp based functions posted above were accurate in that they counted out a hundred words, but recombined the paragraph by imploding an array down to a string. This did away with any such hopes of line breaks, and thus I devised a crude but very accurate function which does all that I ask it to:

I was interested in a function which returned the first few words out of a larger string.

In reality, I wanted a preview of the first hundred words of a blog entry which was well over that.

I found all of the other functions which explode and implode strings to arrays lost key markups such as line breaks etc.

So, this is what I came up with:

The idea behind it? Use str_word_count to identify the nth word, then use str_word_count to identify the position of that word within the string, then use substr to extract up to that position.

This is my own version of to get SEO meta description from wordpress post content. it is also generic usage function to get the first n words from a string.

to count words after converting a msword document to plain text with antiword, you can use this function:

Here is a php work counting function together with a javascript version which will print the same result.

If you are looking to count the frequency of words, try:

This needs improvement, but works well as is.

= ‘http://www.php.net/’ ;
// or use a local file, see file_get_contents() for valid filenames and restrictions.

Источник

substr

(PHP 4, PHP 5, PHP 7, PHP 8)

substr — Возвращает подстроку

Описание

Список параметров

Если string меньше offset символов, будет возвращена пустая строка.

Пример #1 Использование отрицательного параметра offset

Если length положительный, возвращаемая строка будет не длиннее length символов, начиная с параметра offset (в зависимости от длины string ).

Если параметр length опущен, то будет возвращена подстрока, начинающаяся с позиции, указанной параметром offset и длящейся до конца строки.

Пример #2 Использование отрицательного параметра length

Возвращаемые значения

Возвращает извлечённую часть параметра string или пустую строку.

Список изменений

Примеры

Пример #3 Базовое использование substr()

Пример #4 substr() и приведение типов

class apple <
public function __toString () <
return «green» ;
>
>

Результат выполнения данного примера:

Пример #5 Недопустимый диапазон символов

Результат выполнения данного примера в PHP 8:

Результат выполнения данного примера в PHP 7:

Смотрите также

User Contributed Notes 36 notes

For getting a substring of UTF-8 characters, I highly recommend mb_substr

may be by following functions will be easier to extract the needed sub parts from a string:

Coming to PHP from classic ASP I am used to the Left() and Right() functions built into ASP so I did a quick PHPversion. hope these help someone else making the switch

Shortens the filename and its expansion has seen.

### SUB STRING BY WORD USING substr() and strpos() #####

### THIS SCRIPT WILL RETURN PART OF STRING WITHOUT WORD BREAK ###

Drop extensions of a file (even from a file location string)

= «c:/some dir/abc defg. hi.jklmn» ;

?>

output: c:/some dir/abc defg. hi

Hope it may help somebody like me.. (^_^)

PS:I’m sorry my english is too poor. 🙁

If you want to have a string BETWEEN two strings, just use this function:

$string = «123456789» ;
$a = «12» ;
$b = «9» ;

If you need to parse utf-8 strings char by char, try this one:

Be aware of a slight inconsistency between substr and mb_substr

mb_substr(«», 4); returns empty string

substr(«», 4); returns boolean false

tested in PHP 7.1.11 (Fedora 26) and PHP 5.4.16 (CentOS 7.4)

I wanted to work out the fastest way to get the first few characters from a string, so I ran the following experiment to compare substr, direct string access and strstr:

(substr) 3.24
(direct access) 11.49
(strstr) 4.96

(With standard deviations 0.01, 0.02 and 0.04)

THEREFORE substr is the fastest of the three methods for getting the first few letters of a string.

Here we have gr8 function which simply convert ip address to a number using substr with negative offset.
You can need it if you want to compare some IP addresses converted to a numbers.
For example when using ip2country, or eliminating same range of ip addresses from your website 😀

$min = ip2no ( «10.11.1.0» );
$max = ip2no ( «111.11.1.0» );
$visitor = ip2no ( «105.1.20.200» );

I created some functions for entity-safe splitting+lengthcounting:

I needed a function like lpad from oracle, or right from SQL
then I use this code :

Just a little function to cut a string by the wanted amount. Works in both directions.

Anyone coming from the Python world will be accustomed to making substrings by using a «slice index» on a string. The following function emulates basic Python string slice behavior. (A more elaborate version could be made to support array input as well as string, and the optional third «step» argument.)

The output from the examples:
c
cd
cdefg
abcd
abcd
efg

I have developed a function with a similar outcome to jay’s

Checks if the last character is or isnt a space. (does it the normal way if it is)
It explodes the string into an array of seperate works, the effect is. it chops off anything after and including the last space.

I needed to cut a string after x chars at a html converted utf-8 text (for example Japanese text like 嬰謰弰脰欰罏).
The problem was, the different length of the signs, so I wrote the following function to handle that.
Perhaps it helps.

Using a 0 as the last parameter for substr().

[English]
I created python similar accesing list or string with php substr & strrev functions.

About of pattern structures
[start:stop:step]

?>

Using this is similar to simple substr.

Источник

Функции для работы со строками

Для получения информации о более сложной обработке строк обратитесь к функциями Perl-совместимых регулярных выражений. Для работы с многобайтовыми кодировками посмотрите на функции по работе с многобайтовыми кодировками.

Содержание

User Contributed Notes 24 notes

In response to hackajar yahoo com,

No string-to-array function exists because it is not needed. If you reference a string with an offset like you do with an array, the character at that offset will be return. This is documented in section III.11’s «Strings» article under the «String access and modification by character» heading.

I’m converting 30 year old code and needed a string TAB function:

//tab function similar to TAB used in old BASIC languages
//though some of them did not truncate if the string were
//longer than the requested position
function tab($instring=»»,$topos=0) <
if(strlen($instring)

I use these little doo-dads quite a bit. I just thought I’d share them and maybe save someone a little time. No biggy. 🙂

Just a note in regards to bloopletech a few posts down:

The word «and» should not be used when converting numbers to text. «And» (at least in US English) should only be used to indicate the decimal place.

Example:
1,796,706 => one million, seven hundred ninety-six thousand, seven hundred six.
594,359.34 => five hundred ninety four thousand, three hundred fifty nine and thirty four hundredths

/*
* example
* accept only alphanum caracteres from the GET/POST parameters ‘a’
*/

to: james dot d dot baker at gmail dot com

PHP has a builtin function for doing what your function does,

/**
Utility class: static methods for cleaning & escaping untrusted (i.e.
user-supplied) strings.

Any string can (usually) be thought of as being in one of these ‘modes’:

pure = what the user actually typed / what you want to see on the page /
what is actually stored in the DB
gpc = incoming GET, POST or COOKIE data
sql = escaped for passing safely to RDBMS via SQL (also, data from DB
queries and file reads if you have magic_quotes_runtime on—which
is rare)
html = safe for html display (htmlentities applied)

Always knowing what mode your string is in—using these methods to
convert between modes—will prevent SQL injection and cross-site scripting.

This class refers to its own namespace (so it can work in PHP 4—there is no
self keyword until PHP 5). Do not change the name of the class w/o changing
all the internal references.

Example usage: a POST value that you want to query with:
$username = Str::gpc2sql($_POST[‘username’]);
*/

Example: Give me everything up to the fourth occurance of ‘/’.

//
// string strtrmvistl( string str, [int maxlen = 64],
// [bool right_justify = false],
// [string delimter = «
\n»])
//
// splits a long string into two chunks (a start and an end chunk)
// of a given maximum length and seperates them by a given delimeter.
// a second chunk can be right-justified within maxlen.
// may be used to ‘spread’ a string over two lines.
//

I really searched for a function that would do this as I’ve seen it in other languages but I couldn’t find it here. This is particularily useful when combined with substr() to take the first part of a string up to a certain point.

?>

Example: Give me everything up to the fourth occurance of ‘/’.

The functions below:

Are correct, but flawed. You’d need to use the === operator instead:

Here’s an easier way to find nth.

I was looking for a function to find the common substring in 2 different strings. I tried both the mb_string_intersect and string_intersect functions listed here but didn’t work for me. I found the algorithm at http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#PHP so here I post you the function

Here’s a simpler «simplest» way to toggle through a set of 1..n colors for web backgrounds:

If you want a function to return all text in a string up to the Nth occurrence of a substring, try the below function.

(Pommef provided another sample function for this purpose below, but I believe it is incorrect.)

/*
// prints:
S: d24jkdslgjldk2424jgklsjg24jskgldjk24
1: d
2: d24jkdslgjldk
3: d24jkdslgjldk24
4: d24jkdslgjldk2424jgklsjg
5: d24jkdslgjldk2424jgklsjg24jskgldjk
6: d24jkdslgjldk2424jgklsjg24jskgldjk24
7: d24jkdslgjldk2424jgklsjg24jskgldjk24
*/

?>

Note that this function can be combined with wordwrap() to accomplish a routine but fairly difficult web design goal, namely, limiting inline HTML text to a certain number of lines. wordwrap() can break your string using
, and then you can use this function to only return text up to the N’th
.

You will still have to make a conservative guess of the max number of characters per line with wordwrap(), but you can be more precise than if you were simply truncating a multiple-line string with substr().

= ‘Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque id massa. Duis sollicitudin ipsum vel diam. Aliquam pulvinar sagittis felis. Nullam hendrerit semper elit. Donec convallis mollis risus. Cras blandit mollis turpis. Vivamus facilisis, sapien at tincidunt accumsan, arcu dolor suscipit sem, tristique convallis ante ante id diam. Curabitur mollis, lacus vel gravida accumsan, enim quam condimentum est, vitae rutrum neque magna ac enim.’ ;

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque id massa. Duis sollicitudin
ipsum vel diam. Aliquam pulvinar sagittis felis. Nullam hendrerit semper elit. Donec convallis
mollis risus. Cras blandit mollis turpis. Vivamus facilisis, sapien at tincidunt accumsan, arcu

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque id massa. Duis sollicitudin
ipsum vel diam. Aliquam pulvinar sagittis felis. Nullam hendrerit semper elit. Donec convallis
mollis risus. Cras blandit mollis turpis. Vivamus facilisis, sapien at tincidunt accumsan, arcu
dolor suscipit sem, tristique convallis ante ante id diam. Curabitur mollis, lacus vel gravida

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *