Php 从字符串+;忽略<;sup>;内容

Php 从字符串+;忽略<;sup>;内容,php,Php,我有两种类型的字符串: 356你好,世界 6你好,法国!2,5 我用这段代码来提取数字,实际上我只需要开头的数字 $int_image_id = (int) filter_var(get_the_title(), FILTER_SANITIZE_NUMBER_INT); 结果是: 356 625(但应该是6) 如果数字总是作为字符串的前缀,您也可以简单地使用 希望有帮助: $int_image_id = (int) get_the_title(); 如果$int\u image\u id='

我有两种类型的字符串:

  • 356你好,世界
  • 6你好,法国!2,5
  • 我用这段代码来提取数字,实际上我只需要开头的数字

    $int_image_id = (int) filter_var(get_the_title(), FILTER_SANITIZE_NUMBER_INT);
    
    结果是:

  • 356
  • 625(但应该是6)

  • 如果数字总是作为字符串的前缀,您也可以简单地使用

    希望有帮助:

    $int_image_id = (int) get_the_title();
    
    如果
    $int\u image\u id='6你好,法国!2,5';

    输出:

    6
    

    我检查过了。

    另一种方法是使用正则表达式
    preg_match('/\d+/',$string,$matches)
    上行将搜索数字并将其添加到
    $matches
    数组中

    <?php
    $string = '6 Hello France! 2,5';
    
    $matches = array();
    preg_match('/\d+/', $string, $matches);
    print_r($matches)
    ?>
    

    那么
    $int\u image\u id=(int)获取标题()怎么样???
    
    <?php
    $string = '6 Hello France! 2,5';
    
    $matches = array();
    preg_match('/\d+/', $string, $matches);
    print_r($matches)
    ?>