PHP: strpbrk - Manual (original) (raw)
(PHP 5, PHP 7, PHP 8)
strpbrk — Search a string for any of a set of characters
Description
Parameters
string
The string where characters is looked for.
characters
This parameter is case sensitive.
Return Values
Returns a string starting from the character found, or [false](reserved.constants.php#constant.false) if it is not found.
Examples
Example #1 strpbrk() example
`<?php
$text
= 'This is a Simple text.';// this echoes "is is a Simple text." because 'i' is matched first
echo strpbrk($text, 'mi'), PHP_EOL;// this echoes "Simple text." because chars are case sensitive
echo strpbrk($text, 'S'), PHP_EOL;
?>`
See Also
- strpos() - Find the position of the first occurrence of a substring in a string
- strstr() - Find the first occurrence of a string
- preg_match() - Perform a regular expression match
Found A Problem?
11 years ago
If you're not looking to duplicate the rest of the string, but instead just want the offset, in the spirit of the str*pos() functions, use strcspn()guillaume dot barranco at free dot fr ¶
8 years ago
A little modification to Evan's code to use an array for the second parameter :
<?php
function strpbrkpos($s, $accept) {
$r = FALSE;
$t = 0;
$i = 0;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>p</mi><msub><mi>t</mi><mi>l</mi></msub><mo>=</mo><mi>c</mi><mi>o</mi><mi>u</mi><mi>n</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">accept_l = count(</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">a</span><span class="mord mathnormal">cce</span><span class="mord mathnormal">p</span><span class="mord"><span class="mord mathnormal">t</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:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.01968em;">l</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="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">co</span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>accept);
for ( ; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo><</mo></mrow><annotation encoding="application/x-tex">i < </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6986em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">i</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel"><</span></span></span></span>accept_l ; $i++ )
if ( ($t = strpos($s, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>c</mi><mi>c</mi><mi>e</mi><mi>p</mi><mi>t</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">accept[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal">cce</span><span class="mord mathnormal">pt</span><span class="mopen">[</span></span></span></span>i])) !== FALSE )
if ( ($r === FALSE) || ($t < $r) )
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">r = </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" style="margin-right:0.02778em;">r</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>t;
return $r;
}
?>