douzhengzuo7283 2014-12-30 12: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 12:15
    关注
    $myString = '!!@@$IMPORTANT#=-=whatever';
    
    $result = substr($myString, 5, -1);
    
    $pos = strpos($result, '#');
    
    $result = substr($result, 0, $pos);
    
    echo $result;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部