Jeffro Posted May 19, 2011 Share Posted May 19, 2011 I want to basically delete any word that ends with ... So.. If I have the following phrase: Elvis Pres... I want to remove the word Pres... entirely. Basically, something along the lines of... $myphrase = str_replace("Pres...",'',$myphrase); except that I never know what $myphrase will be, so the above won't work. Is it str_replace I need or something else? Quote Link to comment https://forums.phpfreaks.com/topic/236870-how-would-i-str_replace-this/ Share on other sites More sharing options...
volatileboy Posted May 19, 2011 Share Posted May 19, 2011 I can't help you much with this but you need to be using preg_replace because if im not mistaken str_replace will only replace an exact string match Quote Link to comment https://forums.phpfreaks.com/topic/236870-how-would-i-str_replace-this/#findComment-1217589 Share on other sites More sharing options...
fugix Posted May 19, 2011 Share Posted May 19, 2011 im thinking that you can do something where you check to see if the string ends in... if it does, you can use substr() to remove the words that contains the periods Quote Link to comment https://forums.phpfreaks.com/topic/236870-how-would-i-str_replace-this/#findComment-1217592 Share on other sites More sharing options...
AbraCadaver Posted May 19, 2011 Share Posted May 19, 2011 Probably preg_replace (not tested): $new_string = preg_replace('/ [a-zA-Z]+\.\.\./', '', $string); Quote Link to comment https://forums.phpfreaks.com/topic/236870-how-would-i-str_replace-this/#findComment-1217602 Share on other sites More sharing options...
Jeffro Posted May 19, 2011 Author Share Posted May 19, 2011 Thanks to all for the replies. In using Abra's code, it seems to do the trick except for one exception... words that have a hyphen in them. How can I expand to the code to work as it is but also for hyphenated words? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/236870-how-would-i-str_replace-this/#findComment-1217703 Share on other sites More sharing options...
DavidAM Posted May 19, 2011 Share Posted May 19, 2011 Add the hyphen to the list of allowed characters (between the brackets). Since the hyphen has special meaning inside the square brackets, it will have to be escaped or added at the beginning or end: $new_string = preg_replace('/ [a-zA-Z-]+\.\.\./', '', $string); Now, to answer your next question (before you ask it): what about "words" with numbers in them? Add the numbers, or we can change the original suggestion to just catch everything except a space ... $new_string = preg_replace('/ [a-zA-Z0-9-]+\.\.\./', '', $string); // A-Z (upper or lowercase), numbers and hyphen $new_string = preg_replace('/ [^ ]+\.\.\./', '', $string); // Anything that is not a space. In every case, since we are using the "+" qualifier, you will not be getting rid of ellipses that immediately follow a space. If you change the "+" to an "*", you should cover that case as well. Note: This is NOT anchored at the end of the string, if there is something... in the middle of the string, it will go away, too. To anchor it at the end of the string add a "$" to the end of the regexp: $new_string = preg_replace('/ [^ ]+\.\.\.$/', '', $string); Quote Link to comment https://forums.phpfreaks.com/topic/236870-how-would-i-str_replace-this/#findComment-1217721 Share on other sites More sharing options...
Jeffro Posted May 19, 2011 Author Share Posted May 19, 2011 That was a great explanation David. It's the first time I've been able to look at preg_replace and actually be able to understand what it's doing! Many thanks to you all. Quote Link to comment https://forums.phpfreaks.com/topic/236870-how-would-i-str_replace-this/#findComment-1217726 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.