2014-09-06 102 views
0

我想查找字符串內的特殊字符。這些元音是西班牙語的變音符號。該程序應該用帶有重音符號的字符串來統計元音。在西班牙語中,他們是áé,í,ó和ú。儘管我只是第一個4的球員。問題是我的代碼沒有檢測到特殊字符尋找特殊字符php

<?php 
$string='maría'; 

$vocales = preg_match_all('/[aeiouáéíó]/i',$string,$matchesV); 
echo "<br>vocales = $vocales"; 

if ((in_array('á',$matchesV))||(in_array('é',$matchesV)) || (in_array('í',$matchesV)) || (in_array('ó',$matchesV))){ 
    $v = $vocales - 1; 
    echo "<br>v $v"; 
    echo '<br>1'; } 

?> 
+0

你應該使用http://php.net/manual/en/book .mbstring.php – 2014-09-06 17:32:14

+1

只是一個提示:你可以在兩個數組中使用'in_array' - if(in_array(array('á','é','í','ó'),$ matchesV)){ – 2014-09-06 17:37:13

回答

0

爲什麼正則表達式?如果我理解正確的問題,那麼它的一點點變化做this function

<?php 
function substr_count_array($string, $arr) 
{ 
    foreach ($arr as $letter) 
      $count[$letter] += substr_count($string, $letter); 
    return $count; 
} 

$str = 'óóómaóríaéé'; 
print_r(substr_count_array($str, array("á","é","í","ó"))); 

輸出:

Array 
(
    [á] => 0 
    [é] => 2 
    [í] => 1 
    [ó] => 4 
)