PHP: Hypertext Preprocessor (original) (raw)

strcspn

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

strcspn — Find length of initial segment not matching mask

Description

If offset and length are omitted, then all of string will be examined. If they are included, then the effect will be the same as calling strcspn(substr($string, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>o</mi><mi>f</mi><mi>f</mi><mi>s</mi><mi>e</mi><mi>t</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">offset, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.10764em;">ff</span><span class="mord mathnormal">se</span><span class="mord mathnormal">t</span><span class="mpunct">,</span></span></span></span>length), $characters) (see substr for more information).

Parameters

string

The string to examine.

characters

The string containing every disallowed character.

offset

The position in string to start searching.

If offset is given and is non-negative, then strcspn() will begin examining string at the offset'th position. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If offset is given and is negative, then strcspn() will begin examining string at the offset'th position from the end of string.

length

The length of the segment from string to examine.

If length is given and is non-negative, then string will be examined for length characters after the starting position.

If length is given and is negative, then string will be examined from the starting position up to length characters from the end of string.

Return Values

Returns the length of the initial segment of string which consists entirely of characters not in characters.

Note:

When a offset parameter is set, the returned length is counted starting from this position, not from the beginning ofstring.

Changelog

Version Description
8.4.0 Prior to PHP 8.4.0, when characters was the empty string, the search would incorrectly stop at the first null byte in string.
8.0.0 length is nullable now.

Examples

Example #1 strcspn() example

<?php $a = strcspn('banana', 'a'); $b = strcspn('banana', 'abcd'); $c = strcspn('banana', 'z'); $d = strcspn('abcdhelloabcd', 'a', -9); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>e</mi><mo>=</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>c</mi><mi>s</mi><mi>p</mi><mi>n</mi><msup><mo stretchy="false">(</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>a</mi><mi>b</mi><mi>c</mi><mi>d</mi><mi>h</mi><mi>e</mi><mi>l</mi><mi>l</mi><mi>o</mi><mi>a</mi><mi>b</mi><mi>c</mi><msup><mi>d</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><msup><mo separator="true">,</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><msup><mi>a</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo separator="true">,</mo><mo>−</mo><mn>9</mn><mo separator="true">,</mo><mo>−</mo><mn>5</mn><mo stretchy="false">)</mo><mo separator="true">;</mo><mi>v</mi><mi>a</mi><msub><mi>r</mi><mi>d</mi></msub><mi>u</mi><mi>m</mi><mi>p</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">e = strcspn(&#x27;abcdhelloabcd&#x27;, &#x27;a&#x27;, -9, -5);var_dump(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">rcs</span><span class="mord mathnormal">p</span><span class="mord mathnormal">n</span><span class="mopen"><span class="mopen">(</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mord mathnormal">ab</span><span class="mord mathnormal">c</span><span class="mord mathnormal">d</span><span class="mord mathnormal">h</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">o</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">c</span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct"><span class="mpunct">,</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal">a</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">−</span><span class="mord">9</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">−</span><span class="mord">5</span><span class="mclose">)</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mopen">(</span></span></span></span>a); var_dump($b); var_dump($c); var_dump($d); var_dump($e); ?>

The above example will output:

int(1) int(0) int(6) int(5) int(4)

Notes

Note: This function is binary-safe.

See Also

Found A Problem?

AT-HE (at_he AT hotmai1 DOT com)

19 years ago

`this function can be used like strspn(), except while that can be used to compare a string with an allowed pattern, this one can be use to compare a string with a FORBIDDEN pattern

so, to know if any forbidden character has a position inside our string, we can use (not tested with backslashes)...

"; if (strlen($filename) != strcspn($filename,$forbidden)) { echo "you cant create a file with that name!"; }// SHORT VERSION if (strlen($filename) - strcspn($filename,"\"\\?*:/@|<>")) { echo "i told you, you cant create that file"; } ?>

`

maskedcoder at hotmail dot com

19 years ago

useful for finding beginning of quotes and/or tags in a variable containing html. <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>o</mi><mi>s</mi><mo>=</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>c</mi><mi>s</mi><mi>p</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">pos = strcspn(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">os</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">rcs</span><span class="mord mathnormal">p</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>data, '<"\''); will find the first occurance of either the beginning of a tag, or a double- or single-quoted string.

legacyprog at routinz dot net

14 years ago

When you use the third parameter remember that the function will return the number of characters it bypassed, which will *not* be the position in your source string. It's a simple fix to just add your third parameter value to the function result to get the position in the first string where the scan stopped, but I didn't think of it at first.

Anonymous

15 years ago

strcspn() can also be thought of as analogous to the following regular expression: <?php // where ... represents the mask of characters preg_match('/[^ ...]/', substr($subject, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>a</mi><mi>r</mi><mi>t</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">start, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8095em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">t</span><span class="mpunct">,</span></span></span></span>length) ); ?> By this analogy, strcspn() can be used in place of some regular expressions to match a pattern without the overhead of a regex engine -- for example, ways to verify if an input string represents a binary value: <?php preg_match('/^[01]+$/i', $subject); // or... !preg_match('/[^01]/i', $subject);// ...or using strcspn() !strcspn($subject, '01'); ?>

nospam at nospam dot com

8 years ago

`It might not be clear from the example, that

strcspn('abcdhelloabcd', 'abcd', -9, -5) == 4

because it's only evaluating 'hell' which doesn't contain any mask, so returns strlen('hell').

`