Remove words from a phrase and leave only one space between words

I have the following phrase and I need to remove English characters in PHP

$field = "日本語フレーズ A Japanese phrase A Japanese phrase 日本語フレーズ";

I have the following regex

trim(preg_replace('/[a-zA-Z]/', '', $field));

but this will lead to more than one gap between them.

日本語フレーズ       日本語フレーズ

I need to have only one space between them. Below is the expected result.

日本語フレーズ 日本語フレーズ
+4
source share
4 answers

You need to add another function preg_replace.

$str = preg_replace(/[A-Za-z]/, '', $field);
echo preg_replace(/^\h+|\h+$|(\h)+/, '\1', $str);
+1
source
[a-zA-Z ]+

You can try this.Replace by. Watch the demo.

https://regex101.com/r/cK4iV0/16

$re = "/[a-zA-Z ]+/m";  
$str = "日本語フレーズ A Japanese phrase A Japanese phrase 日本語フレーズ"; 
$subst = " "; 

$result = preg_replace($re, $subst, $str);
+3
source

You need to try something like this: -

<?php
$field = "日本語フレーズ A Japanese phrase A Japanese phrase 日本語フレーズ";
$field1 = trim(preg_replace('/[a-zA-Z]/', ' ', $field));
echo trim(preg_replace('/\s+/', ' ', $field1));
?>
+2
source

Try the following:

trim(preg_replace('/[a-zA-Z ]/', ' ', $field));
0
source

All Articles