1

I have asked this before but cant find a response/answer to the same.

I do have tags like this..

news,门户,æ–°é—»,portal,网易,163,china,门户ç

I need to extract only news,portal,163,china from above. how can I do it in php??

hakre
  • 193,403
  • 52
  • 435
  • 836
leon
  • 151
  • 1
  • 7

4 Answers4

2

Replace anything that's not a letter or comma:

$sString = preg_replace('/[^a-z0-9,]+/i', '', $sString);
$sString = preg_replace('/,{2,}/', ',', $sString);
enobrev
  • 22,314
  • 7
  • 42
  • 53
2

Assuming you want to keep only ascii alpha-numeric parts of this string, the following code will work:

$str = explode(',', $str);
$str = preg_grep('#^[[:alnum:]]+$#', $str);
$str = implode(',', $str);

This one too:

$str = preg_replace('#(^([^[:alnum:],]+,)+|,[^[:alnum:],]+)#', '', $str);

Which both return news,portal,163,china

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
0
echo str_replace(',,', ',', preg_replace('~[^a-zA-Z0-9,]~i', '', $searchString));
powtac
  • 40,542
  • 28
  • 115
  • 170
0
s/([[:alnum:]\ ]+,?|) .*? ([[:alnum:]\ ]+,?|) /$1$2/xsg

or

s/[\x{80}-\x{1fffff}]+,?//g
j0k
  • 22,600
  • 28
  • 79
  • 90