Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting digits and latin letters from a string

Tags:

arrays

php

split

Currently I have an array something like this

   [0] => IS-001 開花した才能「篠ノ之 箒」

From this, I would like to extract only the IS-001 part and leave the Japanese character behind to something like this

    [0] => 開花した才能「篠ノ之 箒」

Normal preg_split I am using currently only for white space but it seems like having some issue on the 箒」 character to fall into next array. So I decided if only I can split those non Japanese characters out?

like image 338
user1897151 Avatar asked Aug 11 '15 07:08

user1897151


People also ask

How do you split a string into letters and numbers in Python?

Use the re. split() method to split a string into text and number, e.g. my_list = re. split(r'(\d+)', my_str) .


2 Answers

Try this

echo preg_replace('/^[a-zA-Z0-9\-_]+/u','','IS-001 開花した才能「篠ノ之 箒」');
  • ^ assert position at start of the string
  • [a-zA-Z0-9\-_] match a single character present in the list
  • + Between one and unlimited times, as many times as possible, giving back as needed
  • u modifier unicode: Pattern strings are treated as UTF-16.
like image 52
Narendrasingh Sisodia Avatar answered Sep 28 '22 05:09

Narendrasingh Sisodia


A solution to this is by using multibyte string functions.

So $char = substr($str, $i, 1); will become $char = mb_substr($str, $i, 1, 'UTF-8'); and strlen($str) will become mb_strlen($str, 'UTF-8').

$str="IS-001 開花した才能「篠ノ之 箒」";
$japanese = preg_replace(array('/[^\p{Han}?]/u', '/(\s)+/'), array('', '$1'), $str);

echo $japanese;

(or)

Remove latin letters and digits from string

$res = preg_replace('/[a-zA-Z0-9-]+/', '', $str);
echo $res;
like image 42
Deenadhayalan Manoharan Avatar answered Sep 28 '22 03:09

Deenadhayalan Manoharan