doushan6161 2015-07-23 21:26
浏览 33

PHP - 删除字符串的一部分

I'm new to PHP and I have a problem.

I need delete all chars since a symbol (Sorry for my bad english , i'm from argentina)

I have this text :

 3,94€

And I need the text is as follows:

3,94

I tried this by multiple ways but it didn't work.

  • 写回答

4条回答 默认 最新

  • dtqqq24248 2015-07-23 21:29
    关注

    There are a few ways you can do this:

    Using strpos:

    $string = '3,94€';
    echo substr($string, 0, strpos($string, '&'));
    

    or using strstr:

    // Requires PHP 5.3+ due to the true (before_needle) parameter
    $string = '3,94€';
    echo strstr($string, '&', true);
    

    or using explode:

    // Useful if you need to keep the &#8364 part for later
    $string = '3,94€';
    list($part_a, $part_b) = explode('&', $string);
    echo $part_a;
    

    or using reset:

    $string = '3,94€';
    echo reset(explode('&', $string));
    

    The best suited in your case would be to use strpos to find the first occurrence of & in the string, and then use substr to return the string from the begining until the value returned by strpos.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分