douzhengzuo7283 2014-12-30 20:05
浏览 570
已采纳

在PHP中删除字符串左右两侧的字符

This is an example of a string: abcde123#ijklmn0pq

In that string I need to print out only the numbers (the 123 sequence), and remove the letters (from both left and right) and the hashtag (#) to be removed as well.

The hashtag (#) is always included in the string.

The hashtag (#) is always positioned to the right of the characters that need to be printed;

The hashtag (#) is always positioned to the left of the characters that need to be removed;

Therefore, the hashtag (#) can be used as a guide to remove the letters from the Right

The number of characters in the beginning is always equal to 5 (constant) (to be removed);

The number of characters in the middle is always different (variable) (to be printed);

The number of characters in the right is always different (variable) (to be removed);

Here's another string example, similar to the first one: !!@@$IMPORTANT#=-=whatever

The characters that need to be printed are the word "IMPORTANT"

As with the first example, what's on the left side of the hashtag (#) needs to be printed, but it's important to print only the "IMPORTANT" word, without the special characters "!!@@$".

  • 写回答

4条回答 默认 最新

  • dongsu1951 2014-12-30 20:15
    关注
    $myString = '!!@@$IMPORTANT#=-=whatever';
    
    $result = substr($myString, 5, -1);
    
    $pos = strpos($result, '#');
    
    $result = substr($result, 0, $pos);
    
    echo $result;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?