php preg_replace单独的点

时间:2016-03-18 20:50:44

标签: php regex preg-replace

我有一个脚本,它从字符串给我关键字。代码是:

<?php
$text = "This is some text. This is some text. Vending Machines are great.Баста - ЧК (Чистый Кайф)";
$string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $text);
$string = preg_replace('/\s+/', ' ', $string);
$string = preg_replace('/\s+/', ' ', $string);
$string = mb_strtolower($string, 'UTF-8');
$keywords = explode(' ', $string);
var_dump($keywords);
?>

这很有效,但我遇到了问题。这段代码告诉我:

array (size=15)
  0 => string 'this' (length=4)
  1 => string 'is' (length=2)
  2 => string 'some' (length=4)
  3 => string 'text' (length=4)
  4 => string 'this' (length=4)
  5 => string 'is' (length=2)
  6 => string 'some' (length=4)
  7 => string 'text' (length=4)
  8 => string 'vending' (length=7)
  9 => string 'machines' (length=8)
  10 => string 'are' (length=3)
  11 => string 'greatбаста' (length=15)
  12 => string 'чк' (length=4)
  13 => string 'чистый' (length=12)
  14 => string 'кайф' (length=8)

为什么第11个数组greatбаста。我想分开精彩баста字词。

如果点附近有东西,我需要用代替点和空格(。)的东西。

例子:
这是美好的一天。天气晴朗=&gt;这是美好的一天。晴天(替换为点和空格(。))
这是美好的一天。晴天=&gt;这是美好的一天。阳光灿烂没有取代。因为点后面有空格

2 个答案:

答案 0 :(得分:1)

第一次替换应该用空格进行,最后一次输入应该被修剪。

使用

$text = "This is some text. This is some text. Vending Machines are great.Баста - ЧК (Чистый Кайф)";
$string = preg_replace('/[^\p{L}\p{N}\s]/u', ' ', $text); // <= Replace with space
$string = preg_replace('/\s+/', ' ', $string);
$string = mb_strtolower($string, 'UTF-8');
$keywords = explode(' ', trim($string));        // <= Use trim to remove leading/trailing spaces
var_dump($keywords);

请参阅IDEONE demo

我还猜你不需要重复的$string = preg_replace('/\s+/', ' ', $string);行。

答案 1 :(得分:0)

你只需要2个正则表达式。

查找:#include <iostream> #include<math.h> #include<vector> #include<fstream> using namespace std; vector<double> takestep (double h,double k,vector<double> ukm1,vector<double> ukm2){ int n = ukm1.size(); vector<double> uk(n); uk[0]=0; uk[n-1]=0; for(int i = 1; i < (n-1); i++) { uk[i] = (pow(k,2)/pow(h,2))*(ukm1[i+1]-2*ukm1[i]+ukm1[i-1])+2*ukm1[i]-ukm2[i]; } return uk; } //----------------------------------------------------------------- vector<vector<double> > solve1D (double tf, double h, double k, vector<double> ukm1, vector<double> ukm2){ int n = ukm1.size(); vector<double> t((int)tf/k); for(int i = 0; i<t.size(); i++) { t[i] = k*i; } vector<vector<double> > u(t.size(),vector<double>(n)); u[0] = ukm1; u[1] = takestep(k,h,ukm1,ukm2); for(int i = 2;i<t.size();i++) { u[i]= takestep(h,k,u[i-1],u[i-2]); } return u; } //==================================================================== int main(int argc, char** argv) { double tf = 12.0; double k = .005; double h = .01; vector<double> x(1.0/h); for(int i = 1;i<x.size();i++) { x[i] = i*h; } vector<double> yo(x.size()); yo[0]=0; yo[x.size()-1]=0; for(int i = 1;i<yo.size()-1;i++) { yo[i] = 0.5*sin(x[i]*M_PI); } vector<vector<double> > u = solve1D(tf,k,h,yo,yo); ofstream myfile; myfile.open ("Wave1D_output.txt"); for(int i = 0;i<u.size();i++) { for(int j = 0;j<yo.size();j++) { myfile<< u[i][j]<<"\t"; } myfile<<"\n"; } myfile.close(); return 0; }
替换:没有

查找:[^\p{L}\p{N}\s.]+
替换:空格

然后爆炸。

直接和点 !!

的排序