Jump to content
xisto Community
iGuest

Check For Occurence Of Substring In String

Recommended Posts

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

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 by dserban (see edit history)

Share this post


Link to post
Share on other sites

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 :blink: If RegExTester works properly, so should this regular expression.

Share this post


Link to post
Share on other sites

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 picture
Is 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.