[英]Regex not stripping periods
為什么我的正則表達式沒有去掉句點? 最終結果應該是 output 只有字母和數字字符,加上“-”,但我一直在 output 中得到句點。我試過 trim($string, '.') 但沒有用。 請幫助!
更新。 我已經用正確的解決方案更新了代碼。 謝謝!
<?php
protected $trimCharacters = "/[^a-zA-Z0-9_-]/";
protected $validWords = "/[a-zA-Z0-9_-]+/";
private function cleanUpNoise($inputText){
$this->inputText = preg_replace($this->trimCharacters, '', $this->inputText);
$this->inputText = strtolower($this->inputText);
$this->inputText = preg_match_all($this->validWords, $this->inputText, $matches);
return $matches;
}
?>
您的正則表達式僅在您第一次進行模式匹配時獲取...嘗試在您的模式中設置全局標志,例如
"/[\\s,\\+]+/g"
就像是
'/[\s,\+]+/g'
'/[^\w-]/g'
將是你的表達,你正在尋找......注意:你必須轉義你的反斜杠......如果不是 php 將嘗試解釋\s
\+
\w
...
像使用它
protected $splitPattern = '/[\\s,\\+]+/g';
protected $trimCharacters = '/[^\\w-]/g';
編輯:
哦...您不能將其簡化為:
$this->inputText = preg_replace($this->splitPattern, '', $this->inputText);
$this->inputText = preg_replace($this->trimCharacters, '', $this->inputText);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.