the5thace Posted July 28, 2013 Share Posted July 28, 2013 Sample string: "cats NOT dogs". How would I replace that with "cats -dogs"? Notice there is no space between the -dog but there was a space between NOT dog. This should be applicable to any string. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 28, 2013 Share Posted July 28, 2013 (edited) preg_replace('/ not /i', ' -', $subject); preg_replace('/\\bnot\\b /i', '-', $subject); Edited July 28, 2013 by ignace Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 28, 2013 Share Posted July 28, 2013 try something like this: <?php $mystring = 'abcd NOT def'; $findme = 'NOT '; $new=str_replace($findme, '-', $mystring); echo $new; ?> Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 28, 2013 Share Posted July 28, 2013 looking at ignace's version compared to mine I see I did not require either a space before the not or that the not be at the start of the string. Mine is case sensitive. Hers is not. Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 28, 2013 Share Posted July 28, 2013 To catch the NOT at the start of the string and require a space in front of it using substr try this: <?php $mystring = ' abcNOT def'; $findme = ' NOT '; if (substr($mystring, 0, 4)=='NOT '){ $new="-".substr($mystring, 4); }else{ $new=str_replace($findme, ' -', $mystring); } echo $new; ?> Quote Link to comment Share on other sites More sharing options...
the5thace Posted July 28, 2013 Author Share Posted July 28, 2013 echo still ends with dog+not+cat $Bquery = urlencode($_POST['query']); if ( strpos( strtoupper($Bquery), "NOT")!==false ) { $findme = ' NOT '; if (substr($Bquery, 0, 4)=='NOT ') { $new="-".substr($Bquery, 4); } else { $new=str_replace($findme, ' -', $Bquery); } echo $new; Quote Link to comment Share on other sites More sharing options...
.josh Posted July 28, 2013 Share Posted July 28, 2013 the search/replace logic is failing because you are urlencoding the string before doing the search/replace, which is changing " " to "+". Perhaps you meant to urldecode instead? Or perhaps that urlencode needs to happen after this search/replace. In any case, $Bquery = preg_replace('~(?<=\b)(not\s+(?=\w))~i','-',$Bquery); This will replace "not " with "-" only if there's a word character after it. Examples: "cat not dog" > "cat -dog" "not dog" > "-dog" "cat not" > "cat not" "cat not " > "cat not " "cat not ?&$S" > cat not ?&$S" "cat not 12345" > cat -12345" Quote Link to comment Share on other sites More sharing options...
ignace Posted July 28, 2013 Share Posted July 28, 2013 Hers is not. Hers??? Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 29, 2013 Share Posted July 29, 2013 Hers??? My bad. Profuse apologies. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.