Domanda

Could someone explaine me, why my code gives me nothing back?

<?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);
?>
È stato utile?

Soluzione

You're missing delimiters:

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

Also changed the non-greedy .*? to \w+ where \w is a shorthand to word characters so one or more [a-zA-Z_0-9]

Altri suggerimenti

That's not a well formed regular expression. You need to add delimiters:

<?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);
?>

Check it out in this fiddle: http://codepad.org/WSNCktk0

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top