<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Example of unrelated preceding section</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"This is unrelated preceding output, used to demonstrate separation.\n"</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"PHP code example unrelated to the article content.\n"</span></span><span>;
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p></span><?php<br>
/*<br>
Article Title: How to Skip Specific Sections When Using substr_count for Substring Counting?<br>
*/</p>
<p>// In PHP, the substr_count function is used to count how many times a substring occurs in a string.<br>
// Syntax: substr_count(string $haystack, string $needle, int $offset = 0, int $length = ?): int</p>
<p>// The problem: Sometimes we want to count substring occurrences while skipping certain parts of the string.<br>
// For example, we want to count how many times "apple" appears, but ignore those wrapped in square brackets like "[apple]".</p>
<p>// Example string:<br>
$text = "apple [apple] apple (apple) apple";</p>
<p>// Regular counting:<br>
$total_count = substr_count($text, "apple");<br>
// Output: 5 (including the "apple" inside brackets)<br>
echo "Regular count: $total_count\n";</p>
<p>// Method 1: Use regex to exclude specific sections<br>
// We can use preg_replace to remove the parts we don’t want to count, then use substr_count<br>
$cleaned_text = preg_replace('/<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">.</mi><mo>?</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">.*?</annotation></semantics></math>.??/', '', $text); // Remove content inside brackets<br>
$count_exclude_brackets = substr_count($cleaned_text, "apple");<br>
echo "Count excluding brackets: $count_exclude_brackets\n";</p>
<p>// Method 2: Manually split and count<br>
// For more flexibility (e.g., skipping multiple patterns), split the string and count section by section<br>
$parts = preg_split('/(<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="normal">.</mi><mo>?</mo><mo stretchy="false">?</mo></mrow><annotation encoding="application/x-tex">.*?</annotation></semantics></math>.??)/', $text); // Split by bracketed sections<br>
$count_manual = 0;<br>
foreach ($parts as </span>$part) {<br>
$count_manual += substr_count($part, "apple");<br>
}<br>
echo "Manual split count result: $count_manual\n";</p>
<p>// Summary:<br>
// 1. The substr_count function itself cannot directly skip specific sections; you need regex or string manipulation.<br>
// 2. Using preg_replace to remove unwanted parts is a common method.<br>
// 3. For complex needs, split the string and count in sections to flexibly control which areas to skip.<br>
?><br>
</span>
<?php
// Example of unrelated ending section
echo "This is an unrelated ending output example, used to separate the article from other content.\n";
?>
<span></span>