2013-07-26 159 views

回答

-1

Q1)檢查字符串的第一個數字是否爲1?

A1)preg_match("/^1/", $yourNumber)。 Q2)檢查字符串是否在27-34之間。返回1爲真,0爲假 A1b)確保值爲數字,preg_match("/^1\d*$/", $yourNumber)

Q2)檢查字符串是否在27-34之間。

A2)if($yourNumber >= 27 && $yourNumber <= 34)

+0

謝謝你會測試一下。 – user2609179

+0

解析錯誤:語法錯誤,第24行的C:\ xampp \ htdocs \ address_process.php中出現意外的'preg_match'(T_STRING) – user2609179

+0

我可以知道yourNumber的值和您使用的答案(A1?A1b?) –

-1

要檢查字符串的第一個數字是1,你可以這樣做:

$string = "1fjiofwjoie"; 
if($string[0] == 1) 
{ 
    // First digit is 1 
} 

else 
{ 
    // First digit is not 1 
} 

至於檢查字符串27和34之間,你可以這樣做:

if($string >= 27 && $string <= 34) 
{ 
    // Code here 
} 

如果你指的是字符串的長度,這樣做:

if(strlen($string) >= 27 && strlen($string) <= 34) 
{ 
    // Code here 
} 
-1
if(substr("your_string_here",0, 1)=="1") 
echo "Yes, the first character is 1"; 

if(("the_string">27)&("the_string"<34)) 
echo "Yes, the string lies between 27 and 34"; 
1

如果你試圖做一次兩個檢查,你可以使用正則表達式是這樣的:

$str = 'string to test'; 
if (preg_match('/^1.{26,33}$/', $str)) { 
    // string starts with 1, and is 27-34 characters long 
} 

我使用正則表達式是:

/  the start of the regular expression 
^  the start of the string 
1  a literal '1' 
.  any character 
{26,33} previous item repeated 26-33 times total 
$  the end of the string 
/  the end of the regular expression 

請注意,如果您正在驗證比特幣地址,則也可以從3開始。要允許,你應該[13]更換1在正則表達式:

'/^[13].{26,33}$/' 

還要注意的是正確的地址驗證確實應該比這更徹底,並應覈實校驗,以及防止複製或打字錯誤。

the bitcoin wiki

If you would like to validate a Bitcoin address in an application, it is advisable to use a method from this thread rather than to just check for string length, allowed characters, or that the address starts with a 1 or 3.

0

除非我讀過問題正確,你是想知道,如果在一個字符串的第一個數字是數字1和字符串的長度是一個字符27和34之間。

許多答案都是查看字符串的第一個字符,而不是字符串中的第一個數字。

無論如何,這是一個足夠簡單的功能與測試儀。所有的測試都通過了(即它們正確地確定哪些數字在字符串中是1)。空字符串,不帶數字的字符串,短字符串和長字符串都將返回false。

<?php 

/** 
* Is the first digit in the string the number 1 and that the string's length is between 27 and 34. 
* 
* @param string $string 
* @return bool 
*/ 
function checkString($string) 
{ 
    return preg_match('`(\d)`', $string, $matches) && $matches[1] == '1' && strlen($string) >= 27 && strlen($string) <= 34; 
} 



class tester 
{ 
    public function providePossibleStrings() 
    { 
     return [ 
      ['1', false], 
      ['A1', false], 
      ['21', false], 
      ['A21', false], 
      ['A', false], 
      ['', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZa', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ12', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ123', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ12345', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ123456', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678', true], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY01', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY012', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY0123', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXY', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ0', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ02', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ023', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ0234', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ02345', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ023456', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ0234567', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ02345678', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ023456789', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ234567891', false], 
      ['ABCDEFGHIJKLMNOPQRSTUVWXYZ234567890', false], 
     ]; 
    } 

    public function testCheckString($value, $result) 
    { 
     echo($result == checkString($value) ? 'Pass' : 'Fail'), ' : ', $value, PHP_EOL; 
    } 
} 

$tester = new tester(); 
foreach ($tester->providePossibleStrings() as $testParams) { 
    call_user_func_array([$tester, 'testCheckString'], $testParams); 
} 
相關問題