Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php $string = 'apple banana orange apple'; function countWords(string $string) { $clean = trim(preg_replace('/\s{2,}/', ' ', $string)); if ($clean === '') return []; $word = ''; $counter = []; $strlen = strlen($clean); for ($I = 0; $I < $strlen; $I++) { if ($clean [$I] !== ' ') $word .= $clean[$I]; if ($clean[$I] === ' ' || $strlen-1 === $I) { if (isset($counter[$word])) $counter[$word]++; else $counter[$word] = 1; $word = ''; } } } $red = countWords($string); var_dump($res);
Copy Clear