2009-06-01 43 views

回答

31

使用本:

echo substr_count("abca", "a"); // will echo 2 
0

不知道什麼樣你要找的,但這裏的響應是有可能做到這一點的函數:

function findChar($c, $str) { 
    indexes = array(); 
    for($i=0; $i<strlen($str); $i++) { 
     if ($str{$i}==$c) $indexes[] = $i; 
    } 
    return $indexes; 
} 

它傳遞你的性格尋找和你想看的字符串:

$mystring = "She shells out C# code on the sea shore"; 
$mychar = "s"; 
$myindexes = $findChar($mychar, $mystring); 
print_r($myindexes); 

它應該給你類似

Array (
    [0] => 0 
    [1] => 4 
    [2] => 9 
    [3] => 31 
    [4] => 35 
) 

或東西...

0

如果你將要反覆檢查相同的字符串,它會是聰明有某種線索的,甚至assoc命令陣列它,否則,簡單的方法要做到這一點是...

for($i = 0; $i < strlen($s); $i++) 
    if($s[i] == $c) 
    echo "{$s[i]} at position $i"; 
-1

這樣的事情也適用於這樣的:

$string = 'blabla'; 
$searchitem = 'a'; 
$pieces = explode("$searchitem",$string); 
echo count($pieces).' times ' . $searchitem . ' character(s) is/are used '; 
0

這是爲我工作。請嘗試下面的代碼:

$strone = 'Sourabh Bhutani'; 
$strtwo = 'a'; 
echo parsestr($strone, $strtwo); 

function parsestr ($strone, $strtwo) { 
$len = 0; 
while ($strtwo{$len} != '') { 
    $len++; 
} 

$nr = 0; 

while ($strone{$nr} != '') 
{ 
    if($strone{$nr} != ' ') 
    { 
     $data[$nr] = $strone{$nr}; 
    } 
    $nr++; 
} 

$newdata = $data; 

if($len > 1) 
{ 
    $newdata = array(); 
    $j = 0; 
    foreach($data as $val) 
    { 
     $str .= $val; 
     if($j == ($len -1)) 
     { 
      $newdata[] = $str; 
      $str = ''; 
      $j = 0; 
     } 
     else 
      $j++; 
    } 
} 
$i = 0; 

foreach ($newdata as $val) { 
    if($val == $strtwo) 
    { 
     $i++; 
    } 
} 
return $i; 
} 
相關問題