| 
	
 | 
 Posted by Rik on 05/10/06 06:35 
Jerry Fleming wrote: 
> Hi, 
> 
> I want to strip part of a string that *doesn't* match a patter. But 
> preg_replace does the opposite. For example: 
> 
> $str = 'aa bb'; 
> echo preg_replace('/bb|cc/', '', $str); 
> 
> where aa is unpredictable, so my pattern can only be bb|cc. The 
> problem is, how can I replace aa by negating a pattern? 
 
 
Very difficult with preg_replace. If you know _exactly_ what is allowed, why 
not preg_match() the string and continue working with the output from that 
function? Negating characters, getting parts NOT followed or predeeded by a 
specific string can be done, but negating whole words is as far as I know 
impossible. 
 
For instance: 
 
Allowed are 'cat' & 'dog' & whitespacecharacters. 
$tring = "dog   dogcat\ncowcat"; 
preg_match_all('/(cat|dog|\s)/si', $tring, $matches, PREG_PATTERN_ORDER); 
$allowedstring = implode($matches[1]); 
 
Grtz, 
--  
Rik Wasmus
 
  
Navigation:
[Reply to this message] 
 |