4

たとえば、この文字列(干し草の山)のようなPHP文字列があります。

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";

次に、文字列内で針が発生する順序で PHP 配列を設定したいと思います。だからこれは私の針です:

$needle = array(",",".","|",":");

文字列で針を検索すると、$text次のように出力されます。

Array (
   [0] => :
   [1] => ,
   [2] => .
   [3] => |
   [4] => :
)

これはPHPで達成できますか?

これはこの質問に似ていますが、これはJavaScript 用です。

4

4 に答える 4

1

str_splitここで便利かもしれません

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$needles = array(",",".","|",":");
$chars = str_split($string);

$found = array();

foreach($chars as $char){
  if (in_array($char, $needles)){
    $found[] = $char ;
  }
}
于 2013-09-11T14:07:07.207 に答える
0

これにより、期待される結果が得られます。

     <?php
    $haystack= "here is a sample: this text, and this will be exploded. this also | this one too :)";
    $needles = array(",",".","|",":");
    $result=array();
    $len = strlen($haystack) ;
    for($i=0;$i<$len;$i++) {
        if(in_array($haystack[$i],$needles)) {
            $result[]=$haystack[$i];
        }
    }
    var_dump($result);
?>
于 2013-09-11T14:11:37.197 に答える
0
$string = "here is a sample: this text, and this will be exploded. th
is also | this one too :)";

preg_match_all('/\:|\,|\||\)/i', $string, $result); 

print_r(  array_shift($result) );

使用するpreg_match_all

パターン\:|\,|\||\)

動作中のデモ...

于 2013-09-11T14:11:48.047 に答える