如何在PHP中删除单词

这是我第一次在Stackoverflow中访问,我有一个问题如何删除php中的特定单词?

例如: 我有一个密码

echo $tok = Getstr($result, 'token:',':');

结果是

JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODkwMzQzNDEsIm5iZiI6MTU4OTAzMDc0MSwiaXNzIjoicGF5bWVudHMuYmlnY29tbWVyY2UuY29tIiwic3ViIjoiMTM2NTI2IiwianRpIjoiNmI5OTZjMmItMDJiYi00ZWY5LTk2YzktZmE1ZGU4NzNiZjYxIiwiaWF0IjoxNTg5MDMwNzQxLCJkYXRhIjp7InN0b3JlX2lkIjoiMTM2NTI2Iiwib3JkZXJfaWQiOiI3NDMzOSIsImFtb3VudCI6NTAwLCJjdXJyZW5jeSI6IlVTRCJ9fQ.pgf3o1idIxDz5YHKYlvd7yMk2pG7-wfDI-Hv-AfVA2w x-request-id

I want to remove the 'JWT' and the word 'x-request-id', so that final output will be just eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODkwMzQzNDEsIm5iZiI6MTU4OTAzMDc0MSwiaXNzIjoicGF5bWVudHMuYmlnY29tbWVyY2UuY29tIiwic3ViIjoiMTM2NTI2IiwianRpIjoiNmI5OTZjMmItMDJiYi00ZWY5LTk2YzktZmE1ZGU4NzNiZjYxIiwiaWF0IjoxNTg5MDMwNzQxLCJkYXRhIjp7InN0b3JlX2lkIjoiMTM2NTI2Iiwib3JkZXJfaWQiOiI3NDMzOSIsImFtb3VudCI6NTAwLCJjdXJyZW5jeSI6IlVTRCJ9fQ.pgf3o1idIxDz5YHKYlvd7yMk2pG7-wfDI-Hv-AfVA2w

评论
婚礼礼堂
婚礼礼堂

Use str_replace().

$tok = str_replace('JWT ', '', $tok);
点赞
评论
fomnis
fomnis

有很多可能的方法可以从字符串中删除单词。但是最流行的方式是:

  1. If you know what exactly you want to remove you can use https://www.php.net/manual/en/function.str-replace.php

  2. If you don't know input string but know some pattern you can use https://www.php.net/manual/en/function.preg-replace.php

点赞
评论