0

テキスト内の「コード」を見つけたいのですが、これらのコードには文字と数字を含めることができ、長さを変えることもできます。テキストは次のようになります。

This is a example text, it contains different codes like this one 8de96217e0fd4c61a8aa7e70b3eb68 or that one a7ac356448437db693b5ed6125348.

正しい順序(最初から最後まで)でそれらを見つけてエコーするにはどうすればよいですか?prey_match()には方法があると思いますが、正規表現の作り方がわかりません。追加情報:コードはすべて約30文字の長さで、小文字と数字のみが含まれています。

どんな助けでも本当にありがたいです。ありがとうございました!

4

4 に答える 4

5
preg_match_all("/[a-z0-9]{25,}/", $text, $matches);
print_r($matches);

シンプルですが、あなたのケースでうまくいくはずです。

出力:

Array
(
    [0] => Array
        (
            [0] => 8de96217e0fd4c61a8aa7e70b3eb68
            [1] => a7ac356448437db693b5ed6125348
        )

)
于 2012-08-16T14:24:15.473 に答える
1

次のコードを使用できます。

$string = "This is a example text, it contains different codes like this one 8de96217e0fd4c61a8aa7e70b3eb68 or that one a7ac356448437db693b5ed6125348."
preg_match_all("/[0-9a-z]{30,}/", $string, $matches)

は、すべての一致$matchesを含む配列です。必要に応じて、{30,} をより高いまたはより低い数値に調整できます。それは連続した文字の数です。

于 2012-08-16T14:28:05.160 に答える
0

基本的に、

$words = explode($text);
foreach($words as $word)
{
  if(strlen($word)==30)
    echo $word;
}

#+$* などの文字を削除したい場合は、正規表現を使用する必要があります

編集:Forlan07の答えは明らかに優れています。

于 2012-08-16T14:24:16.067 に答える
0

「ハッシュ」に文字と数字の両方が必要な場合は、次のようなものを試すことができます。

$string = "This is a example text, it contains different codes like this one 8de96217e0fd4c61a8aa7e70b3eb68 or that one a7ac356448437db693b5ed6125348.";

$words = explode(" ", $string);
$hashes = array();

foreach ($words as $currWord)
{
    $hasLetter = false;
    $hasNumber = false;

    for ($index = 0; $index < strlen($currWord); $index++)
    {
        if (ctype_alpha($string[$index]))
            $hasLetter = true;
        else
            $hasNumber = true;
    }

    if ($hasLetter && $hasNumber)
        $hashes[] = $currWord;
}
于 2012-08-16T14:28:27.830 に答える