2014-11-05 91 views
1

我有一串隨機數字。我想輸入一個數字(或字符),並找到它在一個字符串中的位置。在字符串中查找ascii號碼

如果我用這樣的代碼,它工作正常:

<?php 
$string='14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196'; 
$string_to_convert=$_POST["number_to_convert"]; 
$number_to_find=ord($string_to_convert); 
if(strpos($string,$string_to_convert)===false){ 
    echo "not found"; 
} 
else{ 
echo strpos($string,$string_to_convert)+1; 
} 
?> 
<form method="post" action=""> 
<input type="textarea" name="number_to_convert"></input> 
<input type="submit"> 
</form> 

但是當我嘗試使用這樣的代碼:如果有

<?php 
$string='14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196'; 
$string_to_convert=$_POST["number_to_convert"]; 
$number_to_find=ord($string_to_convert); 
if(strpos($string,$number_to_find)===false){ 
    echo "not found"; 
} 
else{ 
echo strpos($string,$number_to_find)+1; 
} 
?> 
<form method="post" action=""> 
<input type="textarea" name="number_to_convert"></input> 
<input type="submit"> 
</form> 

就說明沒有找到這個數字,有的隨機數如果有這樣的ISNT號碼。

那麼會出現什麼問題?

+0

您的代碼似乎很好地工作http://runnable.com/me/VFpaIkcwbzADRrhK – Chris 2014-11-05 17:10:07

+0

「對不起,但您嘗試查看的頁面不存在。」 :) – yamahamm 2014-11-05 17:11:00

+0

試試這個http://runnable.com/VFpbQVz5pPoCf8ww/pi-for-php – Chris 2014-11-05 17:13:36

回答

0

最初,您需要檢查變量的發佈數據是否已設置並寫入邏輯,這樣您纔會知道,您正在提取的數據是否正確。謝謝

if(isset($_POST['number_to_convert'])){ 
    // your logic goes here . . . . . 
    } 

試試這個,如果你仍然面臨問題,讓我知道,不需要猶豫。