Nov 20, 2009

Check For Occurence Of Substring In String

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Check For Occurence Of Substring In String

Habble
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!

Comment/Reply (w/o sign-up)

dserban
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.

 

 

 


Comment/Reply (w/o sign-up)

pyost
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.

CODE
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 wink.gif If RegExTester works properly, so should this regular expression.

Comment/Reply (w/o sign-up)

TavoxPeru
QUOTE(Habble @ Aug 17 2007, 10:43 PM) *
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,

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Occurence Substring


    Looking for check, occurence, substring, string

See Also,

*SIMILAR VIDEOS*
Searching Video's for check, occurence, substring, string
advertisement



Check For Occurence Of Substring In String

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com