Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式,PHP-查找需要更正的单词_Php_Regex - Fatal编程技术网

正则表达式,PHP-查找需要更正的单词

正则表达式,PHP-查找需要更正的单词,php,regex,Php,Regex,我有一长串单词。有些单词有特殊的字母 例如,字符串“现在有$dolar inp$t的rea$l问题” 我有一个特别的字母“$” 我需要尽快找到并返回所有带有特殊字母的单词 我所做的是一个函数,它按空格解析这个字符串,然后使用“for”遍历所有单词并搜索每个单词中的特殊字符。当它找到它时,它将它保存在一个数组中。但有人告诉我,使用正则表达式可以获得更好的性能,我不知道如何使用正则表达式实现它 最好的方法是什么 我是regex的新手,但我知道它能帮我完成这项任务吗 我的代码:(禁止是常量) 该代码暂

我有一长串单词。有些单词有特殊的字母

例如,字符串“现在有$dolar inp$t的rea$l问题” 我有一个特别的字母“$”

我需要尽快找到并返回所有带有特殊字母的单词

我所做的是一个函数,它按空格解析这个字符串,然后使用“for”遍历所有单词并搜索每个单词中的特殊字符。当它找到它时,它将它保存在一个数组中。但有人告诉我,使用正则表达式可以获得更好的性能,我不知道如何使用正则表达式实现它

最好的方法是什么

我是regex的新手,但我知道它能帮我完成这项任务吗

我的代码:(禁止是常量) 该代码暂时有效,仅适用于一个禁止字符

function findSpecialChar($x){
$special = "";
$exploded = explode(" ", $x);
foreach ($exploded as $word){
   if (strpos($word,$forbidden) !== false)
     $special .= $word; 
}
return $special;
}
您可以这样使用:

// Set your special word here.
$special_word = "café";

// Set your sentence here.
$string = "I like to eat food at a café and then read a magazine.";

// Run it through 'preg_match''.
preg_match("/(?:\W|^)(\Q$special_word\E)(?:\W|$)/i", $string, $matches);

// Dump the results to see it working.
echo '<pre>';
print_r($matches);
echo '</pre>';
然后,如果您想替换它,可以使用:

其结果是:

// Set your special word here.
$special_word = "café";

// Set your special word here.
$special_word_replacement = " restaurant ";

// Set your sentence here.
$string = "I like to eat food at a café and then read a magazine.";

// Run it through 'preg_replace''.
$new_string = preg_replace("/(?:\W|^)(\Q$special_word\E)(?:\W|$)/i", $special_word_replacement, $string);

// Echo the results.
echo $new_string;
我喜欢在餐馆吃饭,然后看杂志


我确信可以对正则表达式进行优化,以避免像我在本例中所做的那样在“餐厅”前后添加空格,但这是我相信您正在寻找的基本概念。

我所做的是一个按空格解析此字符串的函数,然后使用“for”检查所有单词并在每个单词中搜索特殊字符。当它找到它时,它将它保存在一个数组中。但是有人告诉我,使用正则表达式我可以获得更好的性能,我不知道如何使用正则表达式来实现它。您当前的解决方案需要多长时间?请将所有信息都放在问题中。您至少需要发布一些示例输入数据和一些您尝试过的代码。但是如果你不这样做的话,它会很快被关闭。哇,现在不用花太多时间,因为我用一个小的输入来运行它。它稍后将运行在一些巨大的东西上,我被告知regex的性能比使用for和解析要好得多。对吗?谢谢。如果我有不止一个我正在寻找的特殊字符,我该怎么办?最初的问题是,我正在寻找已损坏的单词,它们不是字母,而是不同的字符,如%、@、*?诸如此类that@Alex问题是您没有向任何人提供任何需要解析的数据。所以我完全不知道如何帮助你。这是我能做的最好的了&我现在就要说再见了!你只是不能玩,“现在怎么办……”你需要明确地给人们一些可以合作的东西。再见!什么意思?我发布了我需要的数据,以与dolars一起参与示例。。。这就是我所拥有的-损坏的字符串,我的工作是找到损坏的单词。谢谢你的回答,这有助于理解这个概念
// Set your special word here.
$special_word = "café";

// Set your special word here.
$special_word_replacement = " restaurant ";

// Set your sentence here.
$string = "I like to eat food at a café and then read a magazine.";

// Run it through 'preg_replace''.
$new_string = preg_replace("/(?:\W|^)(\Q$special_word\E)(?:\W|$)/i", $special_word_replacement, $string);

// Echo the results.
echo $new_string;