afrojojo Posted February 16, 2010 Share Posted February 16, 2010 I have this code that I used to remove all image tags from a string. $pattern = '/<img[^>]+>/is'; $body = preg_replace($pattern, '', $body); How would I change the pattern to cut everything out of the string($body) after it sees "--". Would like to cut out the "--" as well. Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/ Share on other sites More sharing options...
cags Posted February 16, 2010 Share Posted February 16, 2010 Can we see an example input with expected output please, that always helps immensely with with Regex as it can be difficult to decipher exactly what your after. At it's simplest you would be looking at something along the lines of... '#--.*$#' but I don't really see how that bares any relation to the pattern you say you already have. Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/#findComment-1013023 Share on other sites More sharing options...
afrojojo Posted February 16, 2010 Author Share Posted February 16, 2010 Example input: ex.1 Some text -- Some other text ex.2 Some text----------Some other text Output wanted: In both examples above, I would like the output to be: Some text Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/#findComment-1013248 Share on other sites More sharing options...
cags Posted February 16, 2010 Share Posted February 16, 2010 preg_replace('#-{2,}.*$#', '', $input); Should work to a certain degree. You have a blank line in your first example which is located before the --, yet you don't seem to want it in the output. Your best bet will probably be to call trim on the result of the preg_replace. It will replace anything after 2 or more dashes. Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/#findComment-1013272 Share on other sites More sharing options...
afrojojo Posted February 16, 2010 Author Share Posted February 16, 2010 Doesn't work :'( Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/#findComment-1013280 Share on other sites More sharing options...
premiso Posted February 16, 2010 Share Posted February 16, 2010 preg_replace('#([-]{2,}.*)#s', '', $input) Should work properly. Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/#findComment-1013281 Share on other sites More sharing options...
cags Posted February 16, 2010 Share Posted February 16, 2010 Oops, I forgot the s modifier. I should point out though that other than that the pattern is essentially the same as the one suggested by premiso. Placing the dash in a character class will make no difference, the quantifier will be applied to it regardless. Creating the brackets around it to create a capture group is not really achieving anything, since they go around the whole pattern you are simply making $1 an exact copy of $0, and we aren't even using a back-reference in either the pattern or the replacement anyway. Since .* is a 'greedy' match and the . is a catch all assertion (when you remember the s modifier ) it should always match to the end thus meaning the dollar sign I included is probably not required either. Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/#findComment-1013299 Share on other sites More sharing options...
afrojojo Posted February 16, 2010 Author Share Posted February 16, 2010 Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/192225-preg_replace-help/#findComment-1013318 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.