I have the following phrase and I need in PHP to remove the English Characters
$field = "日本語フレーズ A Japanese phrase A Japanese phrase 日本語フレーズ";
I have the following regular expression
trim(preg_replace('/[a-zA-Z]/', '', $field));
but this will leave with more than a single space between.
日本語フレーズ 日本語フレーズ
I need to have only one space between them. The following is the expected output.
日本語フレーズ 日本語フレーズ
You need to add another preg_replace
function.
$str = preg_replace(/[A-Za-z]/, '', $field);
echo preg_replace(/^\h+|\h+$|(\h)+/, '\1', $str);
Try this:
trim(preg_replace('/[a-zA-Z ]/', ' ', $field));
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));
?>
[a-zA-Z ]+
You can try this.Replace by .See 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);