0

以前にASPクラシックでコーディングしたことがあり、実際には同じではないので、私はPHPに少し慣れています

次のテキストを含む 3 つの文字列があります。

$str1 = "is simply dummy text of the printing $$ 6/4r $$ and typesetting industry"
$str2 = "is simply dummy text of the printing $$ 11/11tr $$ and typesetting industry"
$str3 = "is simply dummy text of the printing $$ 15/6 $$ and typesetting industry"

を取得する6/4rにはどうすればよいですか?11/11tr15/6

  1. 検索のようなものだと思います$$
  2. 次の文字はスペースですか?
  3. 次の文字は数字ですか?
  4. 次の文字は '/' ですか?

これらがすべて当てはまる場合、それを取得6/4rして別の変数に入れたいと思います。

PHPでこれを行うにはどうすればよいですか?

4

4 に答える 4

6

どうですかexplode:

var_dump(explode('$$', $str1));

array(3) {
 [0] => string(37) "is simply dummy text of the printing "
 [1] => string(6) " 6/4r " 
 [2]=>  string(25) " and typesetting industry"
}

したがってtrim($array[1])、常に必要なセグメントが返されます。

于 2012-08-17T12:53:03.683 に答える
2

正規表現:

$str = 'is simply dummy text of the printing $$ 6/4r $$ and typesetting industr';

preg_match('|\$\$(.*)\$\$|',$str,$match);

echo $match[1];
于 2012-08-17T12:53:39.410 に答える
0

preg_matchドル記号の間のすべてのもの (スペースではない) を検索するために使用します

function getValue($str){
    $pattern = '/\$\$\s*([^\$\s]+)\s*\$\$/i';
    if(preg_match($pattern, $str, $match)){
        return $match[1];
    }
    return false;
}

echo getValue($str1);
于 2012-08-17T12:58:42.390 に答える
0

それが最善の方法かどうかはわかりませんが、爆発機能を使用します

http://php.net/manual/en/function.explode.php

$pieces = explode(' \$\$ ', $str1);
//Should contain 6/4r
echo $pieces[1];
于 2012-08-17T12:54:09.983 に答える