iGuest 3 Report post Posted August 18, 2007 Hi, I've been working on a personal messenger, and one of the main problems I'm having is that if there are 10 or so replies to a message, you end up with a subject like this:Re: Re: Re: Re: Re: Re: Re: ..... etc. you get the pictureIs there a function on PHP to check for an occurence of a substring in a string? So I could check if the subject already has a "Re: " in it? It would be really helpful, thanks! Share this post Link to post Share on other sites
dserban 0 Report post Posted August 18, 2007 (edited) You could just use regular expressions to blindly perform the preg_replace() you want without performing the check first.As with other topics, I will give you an example of how to do this in a cygwin bash shell and leave it up to you to understand the principle behind the example and adapt it to your specific situation. CONSOLE ## echo "Re: Re: Re: Re: Re: Re: Subject" | sed 's#Re: .*Re: #Re: #'Re: Subject## echo "Re: Subject" | sed 's#Re: .*Re: #Re: #'Re: Subject## echo "Subject" | sed 's#Re: .*Re: #Re: #'Subject#Note that here it is the greedy behavior of regular expression wildcards that takes hold of the entire repetitive Re: string.Also, remember that preg_replace() is always global (s###g) so you only need one iteration for this function. Putting it in a while loop could cause serious performance problems.In this particular case you do need regular expression power, but if you don't - and you just want to search and replace - you should use str_replace instead. Edited August 18, 2007 by dserban (see edit history) Share this post Link to post Share on other sites
pyost 0 Report post Posted August 18, 2007 With my limited knowledge in regular expressions (which seem to be the best solution) and no idea what this code would produce, my only shot would be something like this. preg_replace('/^(Re: )+\i/' , 'Re: ', $string) To my understanding, this code will replace one or more "Re: "s at the beginning of $string with only one "Re: " (notice the space after the colon). I'm sure you'll see whether this works after some testing If RegExTester works properly, so should this regular expression. Share this post Link to post Share on other sites
TavoxPeru 0 Report post Posted August 19, 2007 Hi, I've been working on a personal messenger, and one of the main problems I'm having is that if there are 10 or so replies to a message, you end up with a subject like this:Re: Re: Re: Re: Re: Re: Re: ..... etc. you get the pictureIs there a function on PHP to check for an occurence of a substring in a string? So I could check if the subject already has a "Re: " in it? It would be really helpful, thanks!You can use regular expresions php functions like ereg_replace or preg_replace, also you can use the php functions str_replace, str_ireplace, substr_replace or strtr.Best regards, Share this post Link to post Share on other sites