简体   繁体   中英

Using str_replace to replace a character and then 1 character after that

I have queried a Minecraft server and this is what it looks like "§f§6§lMINE§b§lHEROES §7[1.7-1.14+] §b20% OFF SALE §e§lPRISON SERVER RESET §f- §9§lVIP OUT NOW!"

So I tried using str_replace to get rid of these § .

str_replace('§', '', $server->name)

Now it looks like this "f6lMINEblHEROES 7[1.7-1.14+] b20% OFF SALE elPRISON SERVER RESET f- 9lVIP OUT NOW!"

It did get a lot of the jibberish out, but now there are random letters. These random letters are right after the § symbol. So I need to str_replace that symbol, as well as 1 character after that.

For your question you can try this:

$server->name = preg_replace('/§./', "", $server->name);

This will remove "§" with additional character as you ask.

But you might have to better understand "§" values and find custom solution.

You can explode the string by § , then implode them from index 1. Demo

$arr = explode("§",$str);
$result = array_reduce($arr,function($a,$b){return $a . substr($b,1);},"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM