【问题标题】:Exporting a preg_match into an array将 preg_match 导出到数组中
【发布时间】:2014-11-17 04:37:45
【问题描述】:

嘿,我正在尝试将所选内容准确地放入数组中,但它不起作用。这是我得到的:

$stripped = "listing requirements. $ 669,647,431,200 P/E Ratio: 17.75 Forward P/E(1y) A widel";
$pattern = '/P\/E+\s+\w\w\w\w\w\W\s\w\w\W\w\w/';
preg_match($pattern, $subject, $matches);
print_r ($matches);

基本上我希望它只将“P/E Ratio: 17.75”输出到一个数组中。没有别的了。

我意识到如果市盈率是 114.13,我的表格也不起作用。我怎样才能让它选择接下来的两个“单词”?

谢谢!

【问题讨论】:

    标签: php regex pattern-matching preg-match


    【解决方案1】:

    您可以将正则表达式简化为

    P\/E Ratio: [\d.]+
    

    例如:http://regex101.com/r/tK9rR0/1

    $subject = "listing requirements. $ 669,647,431,200 P/E Ratio: 17.75 Forward P/E(1y) A widel";
    $pattern = '/P\/E Ratio: [\d.]+/';
    preg_match($pattern, $subject, $matches);
    print_r ($matches);
    

    会给出输出

    Array ( [0] => P/E Ratio: 17.75 )
    

    【讨论】:

    • 谢谢!这段代码超级简单,运行完美。
    猜你喜欢
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多