【问题标题】:How do I get a specific string value from a string in PHP? [duplicate]如何从 PHP 中的字符串中获取特定的字符串值? [复制]
【发布时间】:2019-05-21 13:10:10
【问题描述】:

我必须得到一个在字符串中包含“#”的值。

Ex:
$string = '#1234 Lorem ipsum is placeholder text commonly used in the graphic, #rm1345 print, and publishing industries for previewing layouts and visual mockups';

Expected results:
 #1234 and #rm1345

我应该如何从 $string 中得到上述结果?

我尝试了以下方法,但它对我没有帮助。

$contents = str_word_count($string, 2);
echo 'contents: <pre>';print_r($contents);echo '</pre>';

请帮帮我。

谢谢!

【问题讨论】:

    标签: php string


    【解决方案1】:

    这是你的正则表达式和生成的字符串

    $string = '#1234 Lorem ipsum is placeholder text commonly used in the graphic, #rm1345 print, and publishing industries for previewing layouts and visual mockups';
    
    $temp = preg_match_all('/(?<!\w)#\w+/', $string, $matches);
    
    echo implode(' and ', $matches[0]);
    

    输出:

    #1234 and #rm1345
    

    工作demo

    正则表达式测试link.(preg_match_all 匹配所有# 个附加词)。

    来源link

    【讨论】:

    • 有效!谢谢!
    • @RameshMoorthy 欢迎您。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多