0

PHPを使用して文字列内の特殊文字の出現回数を見つける方法を知る必要があります

これがコードです...

<?php

$message=$_POST['Message'];
//$rep=preg_replace("@ # $ % ^ & / . * @"," ",$message);
//$rep=preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $message);
$rep = preg_replace("/[^a-zA-Z]+/", "", $message);
echo "There are ".str_word_count($message)." words found in the given message"."<br/>";

$len=strlen($rep);
echo "length of the message is ".$len;
$delims="?#";
$word = strtok($message, $delims);

echo "delim ".$word."<br/>";
echo $delimlen=strlen($word)."<br/>";

echo "without special ".$rep;


?>
4

2 に答える 2

3

この正規表現を使用して、これが役立つかどうかを確認してください

$pattern = '/[!@#$%^&*()]/'  // will match one occurrence of any symbol inside the []

また

$pattern =  '![^A-z0-9]!i'

preg_match_all

int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

文字列に対してグローバル正規表現の一致を実行します。パターンで指定された正規表現に一致するものをすべて検索し、フラグで指定された順序で一致させます。

最初の一致が見つかった後、後続の検索は最後の一致の終わりから続行されます。

于 2013-03-11T12:49:41.987 に答える
0

交換された部品の数を取得したい場合は、次のようにpreg_match呼び出し用にさらに2つのパラメーターが必要です。

$replaceCount = 0;
preg_replace("/[^a-zA-Z]+/", "", $message, -1, $replaceCount);
于 2013-03-11T12:55:01.573 に答える