How to use PHP to remove end of the string of characters

Foreword

Met a demand on the job: a bunch of strings, such as "Thunder official download", "Nora 5.0 download", you need to remove them at the end of the "official download" and "download", and other characters.

case

case1: str_replace()

I first began to think of is to use str_replace function. However, there is a problem, this function can filter out the substring, although the number of filters may be defined, but can not define the end of the string filter, so pass out.

case2: mb_strpos + mb_substr() + mb_strlen()

The idea is this: are acquiring filter string length, "the official download" is four characters, then use mb_substr cut at the end of four.
php function filter_word($word){ $filterList = ['官方下载','下载']; foreach ($filterList as $fitler){ $wordLen = mb_strlen($word,'utf-8'); $filterLen = mb_strlen($fitler,'utf-8'); $offset = $wordLen - $filterLen; //如果待过滤字符串小于过滤字符串跳过(后期优化加上的) if ($offset <= 0){ continue; } $strPos = mb_strpos($word, $filter, $offset,'utf-8') !== false ? mb_strpos($word, $filter, $offset,'utf-8') : false; if ($strPos !== false){ $word = mb_substr($word,0,$offset,'utf-8'); } } }

note

  • The third parameter mb_strpos function, $ offset is negative does not begin from the end date!
  • Use mb_ * to handle Multibyte String

case3: trim()

case2 can solve this demand, but find it very troublesome, and asked my colleagues, my colleagues have no better way.

Colleagues: "can trim"

:( I thought, 'trim was the first I pass out function ah.') Trim Chinese word garbled.

Colleagues: For Chestnut

I:. . . . .

Then I went to the official website read it carefully trim function , scored a major victory.

  • The second parameter is not specified, the default filtering whitespace and \ t \ n \ r \ 0 \ x0B
  • trim('bacabccba','abc') --> ''
  • trim('abcdefg','a..e') --> g
  • Filter array array_map ( 'trim', $ arr)

But will trim Chinese garbled reasons for not found by Google finally found, inside principle very clear, I will not go into details.

principle

Principle does not explain how to generate a hexadecimal value, here to be complementary: BIN2HEX ()

In addition the official website also suggests that, in addition to trim, split and splice function will function similar problems

case4: preg_replace

In the search for trim garbled in the process, we found that you can also use regular replacement. There were probably confined to this demand, thinking it becomes narrow.

preg_replace('/下载\$/','',$str);

in conclusion

Use regular replacement at the end of the string is the easiest solution.

But by case2, to use a lot of their own function and consider how to optimize a demand to solve feeling is also very good (do not know if that is not the algorithm, ha ha).

Through this demand also summed up before his mind fuzzy memories of "trim Chinese garbled" in.

Thanks demand!

Guess you like

Origin www.cnblogs.com/luyuqiang/p/how-to-filter-the-end-of-the-characters.html