duanmao9918
2015-02-06 12:59
浏览 29
已采纳

获取两个其他字符串之间的字符串

I have this total USD100.75 from string.I am getting the next word like this

$s = 'total USD100.75 from';
$r = preg_match_all('/(?<=(total))(\s\w*)/',$s,$matches);
print_r($matches[0]);

However i would like to get the string between two specific strings i.e total and from. How may i get the middle string?.

图片转代码服务由CSDN问答提供 功能建议

我从字符串得到这个总计USD100.75。我得到的下一个字就像 这个

  $ s ='总计USD100.75来自'; 
 $ r = preg_match_all('/(?&lt; =(total))(\ s \ w *  )/',$ s,$ matches); 
print_r($ matches [0]); 
   
 
 

但是我想获取两个特定字符串之间的字符串 即 total 来自。 我怎么能得到中间字符串?。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dpir3950 2015-02-06 13:01
    已采纳

    You can also use regexp for that, eg.

    '/total (.*?) from/'
    
    点赞 打赏 评论
  • doulu7174 2015-02-06 13:03

    You can explode your string by space.

    $s = 'total USD100.75 from';    
    $s1 = explode(" ", $s);
    echo $s1[1];
    

    Output

    USD100.75
    

    Demo

    点赞 打赏 评论
  • duanjiao6731 2015-02-06 13:04

    Using substr and strlen:

    echo substr($string, strlen("total "), -strlen(" from"));
    
    点赞 打赏 评论
  • duan5991518 2015-02-06 13:05

    The regular expressions are easy way to do it. You just need to understand the basics. You will achieve what you want with the following pattern:

    total\s(.*)\sfrom
    
    点赞 打赏 评论
  • dqprf0976 2015-02-06 13:08

    @user3272483, Please check below answer for your solution.

    $matches = array();
    preg_match("/total (.*?) from/", 'total USD100.75 from', $matches);
    print_r($matches);
    
    点赞 打赏 评论
  • doushai4890 2015-02-06 13:14

    I'm not sure of the specifics of your language, but in Java:

    (?USD[0-9]+\.[0-9]{2})
    

    would work.

    1. "USD", followed by
    2. One or more numerals, followed by
    3. Literal ".",
    4. followed by two numerals.

    If you have commas, you could try:

    (?USD[0-9]{1,3}(,?[0-9]{3})*\.[0-9]{2})
    
    1. "USD" followed by 1-3 numerals, followed by,
    2. any number of groups of: an optional comma followed by 3 numerals, followed by
    3. a decimal, followed by
    4. two digits
    点赞 打赏 评论

相关推荐 更多相似问题