PHP Preg_Replace,“以...开头”不会工作

时间:2014-01-03 07:16:43

标签: php preg-replace startswith

有人可以解释我,为什么我的代码没有给我任何回复?

<?php
    $text = '<article about="about_text" typeof="sioc:Item foaf:Document" class="node node-bildergalerie node-published node-not-promoted node-not-sticky author-lalala odd clearfix" id="node-bildergalerie-6835">';
    $search_for = '(author\-.*?)\s';
    $replace = '';
    print preg_replace($search_for, $replace, $text);
?>

2 个答案:

答案 0 :(得分:2)

你错过了delimiters

$search_for = '~(author\-\w+)\s~';

同时将non-greedy .*?改为\w+ \w shorthand为{{3}}个字符,因此一个或多个[a-zA-Z_0-9]

答案 1 :(得分:1)

这不是一个结构良好的正则表达式。您需要添加分隔符:

<?php
    $text = '<article about="about_text" typeof="sioc:Item foaf:Document" class="node node-bildergalerie node-published node-not-promoted node-not-sticky author-lalala odd clearfix" id="node-bildergalerie-6835">';
    $search_for = '/(author\-.*?)\s/';
    $replace = '';
    print preg_replace($search_for, $replace, $text);
?>

在这个小提琴中查看:http://codepad.org/WSNCktk0