2015-04-30 21 views
0

我想創建一個程序來確定一個字符串是否是迴文。檢測迴文中的php

這是我得到的錯誤。

聲明:Array對C字符串轉換:\ WAMP \ WWW \ task18.php上線22

我的代碼如下:

<?php 

//TASK 18 PALINDROME 
//use string split function to split a string into an array 

$str = "Mum"; 
$str =strtolower($str); 

$strArray = array(); 
$strArray = str_split($str); 

$len = sizeof($strArray); 
$reverseStr =""; 

for($i=$len-1; $i>=0; $i--){ 
    $reverseStr .=$strArray[$i]; 
} 
if ($strArray == $reverseStr) { 
    echo " $strArray is a palindrome"; 
} else { 
    echo " $strArray is not a palindrome"; 
} 
+0

什麼線是22? – Albzi

+0

echo「$ strArray不是迴文」; – Phill

+0

你不能回顯數組。 – Med

回答

1

首先,你」重新比較字符串($reverseStr)與數組($strArray)。

您需要編輯的代碼如下:

for($i=$len-1; $i>=0; $i--){ 
    $reverseStr[] .=$strArray[$i]; 
} 

這會然後把顛倒字到一個數組。所以mum將被正確輸出爲mum,並且測試將會是tset,但是在一個數組中。

然後這將使if通過,但不能回顯數組,因此您應該只回顯出$str

全碼:

$str = "mum"; 
$str =strtolower($str); 

$strArray = array(); 
$strArray = str_split($str); 

$len = sizeof($strArray); 
$reverseStr = array(); 

for($i=$len-1; $i>=0; $i--){ 
    $reverseStr[] .=$strArray[$i]; 
} 
if ($strArray == $reverseStr) { 
    echo "$str is a palindrome"; 
} else { 
    echo "$str is not a palindrome"; 
} 

,或者如果你需要使用$strArray是在echo,您可以使用implode()

echo implode($strArray). " is/is not a palindrome"; 

如果你想讓它更短,您可以使用這個:

$str = strtolower("Mum"); 
$strArray = str_split($str); 
$len = sizeof($strArray); 
$reverseStr = array(); 
for($i=$len-1; $i>=0; $i--) 
    $reverseStr[] .=$strArray[$i]; 
echo "$str is ".($strArray==$reverseStr ? "" : "not") . " a palindrome"; 
+0

for this loop for this loop $ reverseStr []。= $ strArray [$ i]; 。=是什麼意思。這是否意味着+ = – Phill

+0

'。='將某些東西附加到字符串中。例如:'$ str =「Hello」; $ str。=「there!」;'當'$ str'被回顯時,它會是'Hello there!'。如果沒有'.',那就只是'在那裏!'@Phill – Albzi

1

描述: - 你不能回聲列y這就是爲什麼你會得到這個錯誤。因爲你正在迴應一個不可能的數組。類型雜耍(變量是一段時間自動轉換爲最適合的)。這與你的代碼一樣,因爲你試圖回聲一個數組,當php試圖將其轉換爲字符串它失敗。下面是使用str_split()檢查palidrome的代碼。

<?php 
$word = strtolower("mum"); 
$splitted = str_split($word); 
$reversedWord = ""; 
$length = strlen($word); 
for($i = 0; $i < $length; $i++) 
    $reversedWord .= $splitted[$length - $i - 1]; 
echo $word == $reversedWord ? "It's a palindrome " : "It's not a palindrome"; 
?> 
+0

您的代碼與原件完全相同。你沒有改變任何東西。 – Albzi

+0

變化是行22中的字符串var。 – obrvo

+0

@beatalex現在我已經改變了它。 –

0

你也可以使用給定的東西,而不使用任何PHP函數。

$str="level"; 

for($i=0;$i<40000;$i++) 

    if($str[$i]) 
     $count++; 
    else 
    break; 


    for ($j=$count;$j >=0; $j--){ 
     $newStr.=$str[$j]; 
    } 
    //echo $newStr; 

     if($newStr==$str) 
      echo $newStr." is a palindrome"; 

      else 

      echo $newStr." is not a palindrome"; 

?> 
0

你可能想試試這個,它的工作原理...

function fn_palindrome($palindrome) { 
    $reversed = ''; 
    $original = $palindrome; 
    $string = array(); $j = 0; 
    $converted = (string) $palindrome; 
    $palindrome = str_split($converted); 
    $i = count($palindrome) - 1; 
    while($i >= 0) { 
     $string[$j] = $palindrome[$i]; 
     $j++; $i--; 
    } 
    $reversed = implode('', $string); 
    if($reversed == $original) { 
     return TRUE; 
    } else { 
     return FALSE; 
    } 
}