tobeyt23 Posted December 14, 2008 Share Posted December 14, 2008 How do I find the word For in a string and delete it and everything after it with a regular expression? I suck at reg exp and need help thanks! Link to comment https://forums.phpfreaks.com/topic/136951-solved-regular-experssion/ Share on other sites More sharing options...
tobeyt23 Posted December 14, 2008 Author Share Posted December 14, 2008 I think this is correct: preg_replace('/For(.+$)/', '', $string; Link to comment https://forums.phpfreaks.com/topic/136951-solved-regular-experssion/#findComment-715266 Share on other sites More sharing options...
nrg_alpha Posted December 14, 2008 Share Posted December 14, 2008 I think this is correct: preg_replace('/For(.+$)/', '', $string; Actually, it isn't for the simple reason you have not closed off the preg_replace function. You are missing the closing ')' character. As for the pattern itself, I would go this route: preg_replace('#for(.*)#i', '', $string); In your case, you are looking for a For (capital F). Is this deliberate? Or do you need to find any instance of this, regardless whether it contains capital letters or not? If caps are not an issue, then I used the i modifier after my closing delimiter so that it is all case insensitive. I used the 'zero or more' quantifier (*) just in case (for some strange reason) the last characters in the string is 'for' (or any cap variation of it). Since it is a greedy quantifier by default, it will match all the way to a newline. Link to comment https://forums.phpfreaks.com/topic/136951-solved-regular-experssion/#findComment-715322 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.