Jump to content

Matching everything except some selected words


Rowno

Recommended Posts

<pre>
<?php
echo $str = 'apple orange banana strawberry kiwi grapefruit cherry grape';
echo '<hr>';

$keeps = array(
	'range',
	'straw',
	'grape',
	'orangutan',
);

$pattern = '/(' . join('|', $keeps) . ')/';

$pieces = preg_split($pattern, $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r(preg_grep($pattern, $pieces));
?> 
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.