Check For Occurence Of Substring In String

free web hosting
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!

Reply

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.

 

 

 


Reply

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.

Reply

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,

Reply


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*

Recent Queries:-
  1. substring in the occurence of the character - 538.86 hr back. (1)
Similar Topics

Keywords : check, occurence, substring, string

  1. String Library Functions
    A few questions (4)
  2. Check Server Headers
    (3)
    The Check Server Headers tool is another helpful online tool that you can use to check and verify
    HTTP status codes of any page. Server headers and HTTP status codes are a matter of particular
    importance in ensuring that search engines see your content. But checking them can be a bit tricky.
    The Check Server Headers tool is very simple and easy to use, you just need to enter a valid URL,
    and the tool will give you one of seven HTTP status codes. It will also -and this is important- give
    you an interpretation of the code. QUOTE Check server headers and verify HT....
  3. Firefox Addon Check
    (2)
    I saw a site that check if i have a ad-block addon in firefox but cant find something like that on
    google or similar. I know it exist php scripts that show what browser I have CODE $useragent =
    $_SERVER ; if (preg_match(‘|MSIE ( . {1,2})|’,$useragent,$matched)) {
        $browser_version=$matched ;     $browser = ‘IE’; } elseif (preg_match( ‘|Opera
    ( . {1,2})|’,$useragent,$matched)) {     $browser_version=$matched ;     $browser =
    ‘Opera’; } elseif(preg_match(‘|Firefox/( +)|’,$useragent,$matched)) {
            $br....
  4. Dominions 3
    The best game ever (No, really. Check this out) (0)
    Dominions 3 is beautifully complex strategy game brought to you by two guys in their garage, or
    Illwinter studios as they like to be called. It offers a variety of ways to best your opponent,
    swinging some impressive numbers in the stuff department. Last time I checked there were around 1000
    different units; a really large amount of magic spells and wizardry; 50 nations; perhaps a million
    different craftable weapons; and plenty of badass heroes to give those weapons to. The game plays
    sort of like risk in that you move armies around a map divided up into territories, con....
  5. String Theory
    (9)
    I've heard of string theory, and I know that it's baisically the belief that multiple
    universes exist in parallell and only gravitons can travel between them. My question is: Do you
    believe in String Theroy, and what do you know about it that you could teach me?....
  6. Amazing Results From Reverse Ip Domain Check Tool
    (7)
    Here is a reverse IP domain check tool that takes the domain name or IP address of a web server and
    searches for other sites known to be hosted on that same web server. Data is gathered from search
    engine results, which is not guaranteed to be complete. Knowing the other web sites hosted on a web
    server is important from both an SEO and web filtering perspective, particularly for those on shared
    web hosting plans. I tried it with dserban.astahost.com, and I was impressed with the results.
    http://www.yougetsignal.com/tools/web-sites-on-web-server/ ....
  7. Strange Ascii Code 22 Character Detected In Connection String
    (9)
    Warning: Unexpected character in input: '' (ASCII=22) state=1 in
    /usr/local/cpanel/cgi-sys/php4 on line 928 Parse error: syntax error, unexpected T_STRING in
    /usr/local/cpanel/cgi-sys/php4 on line 928 i get that when i get on to my site:
    http://eggie.sphosting.com how to solve it?....
  8. Script To Check Local User Accounts
    (1)
    Hi all, I am new to scripting and I've been asked to create a script that when run, will check
    the Local users on the machine and report back with the username & what group memberships that
    account has. This will be run about 20-30 Windows Server 2003 Enterprise/Standard servers, I have
    novice level of scripting so if anyone can help that would be great! Thanks, Al P.S. If there are
    any other details you need to know just ask /biggrin.gif" style="vertical-align:middle" emoid=":D"
    border="0" alt="biggrin.gif" />....
  9. Firefox 2.0+ Spell Check Tweak
    Spell check all text fields. (0)
    Firefox 2.0+, by default, spell checks only multi-line text areas. To spell check single line text
    fields such as search boxes you typically have to right click and click "spell check this field" in
    the context menu. The following tweak will make it so that spell check is always on by default for
    ALL text fields. 1. Open a new tab and type about:config in the address bar and press Enter.
    2. In the filter field type layout.spellcheckDefault 3. Double click on that entry and enter
    2 into the pop up box. 4. Click OK. You're done /smile.gif" styl....
  10. Firefox Spell Check Tweak.
    (0)
    Get Firefox to check spelling in any text field. By default Firefox only checks spelling in
    multi-line text fields. in the address bar type about:config and hit enter type
    layout.spellcheckDefault in the filter field double click on the entry and in the popup box enter
    2 click ok Now spell check should work in any text field.....
  11. Linux - Open Printing Compatibility Check By Manufacturer
    (0)
    USB printers for the most part work really well under Linux, but there are some that don't work
    at all - they might as well be paperweights, so before you buy a printer, definitely go to:
    http://www.openprinting.org/printer_list.cgi ... they have a very good, up-to-date database of
    printers. It's a dropdown box by manufacturer, so you click your way through it and the verdict
    is like a thumbs up or thumbs down for Linux compatibility - some printers work great, and some
    don't work at all ... and some grey areas inbetween. Additional information can be fo....
  12. The Best Free File Hosting Site !
    Check This Out (6)
    Hi, I found i nice site I really use this because it acts like a usb flash drive because your
    the only one who can access it unlike rapidshare and megaupload I know your aksing for it and here
    it is http://www.dropboks.com It is the most simplest of all file hosting sites you can upload
    up to 50MB and 1GB storage a month and unlimited bandwidth so you can get most of it check it out
    !!!....
  13. Php Any Variable In String.
    (1)
    OK well I am making a new php program and I am trying to add bbcode to it. Anyway I was going to
    replace each thing by it self, but that could cause errors. Anyway is there a way to make a variable
    be anything? Here is an example: CODE This is the bbcode: Hey In my php: $bbcode = array(" ");
    $html = array( " "); $topic_content = str_replace($bbcode, $html, $posted_bbcode); So I want $a
    to be any variable but the same variable as used before. Do you get what I am saying? It is a little
    confusing but basicly what is the best way to make bbcode? It may not even inv....
  14. Sorting A String Vector
    (0)
    I did this code for a reason... It was a school project... I could not find this info anywhere...
    Maybe it will be use to some1 else. /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
    alt="smile.gif" /> #include #include #include using namespace std; int main() { int
    count=0; vector Names(2); Names ="Alexa"; Names ="Alexc"; Names ="Alexb"; cout while(count
    { cout count++; } int i,j; int min; for(i=0;i { min = i; for (j = i + 1; j { if
    (Names Names .swap(Names ); } count=0; cout while(count { cout count....
  15. Help Spliting A String Into 3 Variables In C++
    I have searched on the net but havent found much help (2)
    Hi there im new around here ^^, my original language is spanish so sorry for any wrong word i
    use,... Im working on some features for my program to load a Favorite games list from a text file
    but the problem I have is not parsing the file, is processing a string and spliting it into 3
    variables, then I will use them to insert as items in a List View control, I just dont know what is
    wrong in the following code... CODE
    //------------------------------------------------------------             TCHAR* pszRomname =
    NULL;         TCHAR* pszTitle= NULL;         TCHAR* p....
  16. How To Remove Query String Using Regular Expressions
    (4)
    Hi, I'm a complete newbie using regular expressions and what i want to do is to remove all the
    parameters of the query string of any url using regular expressions, how can i do this? For example
    if i have some urls like these: http://www.domain.com/file1.php?var1=value1&var2=value2
    http://www.domain.com/file2.php?var1=value1&var2=value2
    http://www.domain.com/file3.php?var1=value1&var2=value2 I want to only get these ones:
    http://www.domain.com/file1.php http://www.domain.com/file2.php http://www.domain.com/file3.php
    If it is possible please post no....
  17. Php String To Int Typecasting
    (6)
    I've been working on writing this program that needs strings to be converted to integers.
    Sometimes PHP works by doing the converting for me, but sometimes it just breaks down and quits
    working. I've got no idea why. $sub1 = 0+substr($text, 0, 3); $sub2 = 0+substr($text, 3, 6);
    $sub3 = 0+substr($text, 6, 9); $sum = 0+$sub1+$sub2+$sub3; echo $sum; Basically, $text is a string
    of numbers that looks something like: 296294255268313 and so on. Then I use substr to get the first
    three digits/letters of $text (296) and then the second three digits/letters of $text(2....
  18. Firefox 2 Has A Spell Check!
    Yea, it really does. (40)
    You are probably thinking from the get go that this is something that I should be discussing in the
    software forums, but honestly, it is something that I just discovered and its something new that I
    just learned, so it fits right in here as well. At least, technically it does! Anyways, I loaded up
    my Mozilla Firefox browser a few days back and an upgrade notice popped up. Normally, I don't
    think I have enough time to deal with the download and installation part so I just close it down
    without upgrading, but not today! Instead, I upgraded the browser and it took me....
  19. Check Out This Interesting Information I Found.
    Science research. (0)
    QUOTE Bone, Knee, Hip and Joint Advances "Recommended. The information in this program, as well
    as the entire series, is up-to-date and of interest to both general audiences and to medical
    specialists." -EMR Online "Osteoporosis" - a new diagnostic device screens for signs of weakening of
    the bones; "See Through Bones" - a new x-ray machine which offers doctors extremely accurate details
    of bones; "Dem Bones" - lasers can now be used to stimulate bone growth; "Thinking Knee" - a new
    artificial knee is computerized, meaning amputees will find walking easier; "Artifici....
  20. Image Drives!
    Check this out! (8)
    http://www.daemon-tools.cc/dtcc/download.php This is a link to a free virtual drive creator.... it
    can run .iso images without having to burn them to a CD... simply open the program and choose a file
    to run ^^ Have fun!....
  21. Check This Laptop Out
    I want this for my Birthday ;) (5)
    Ok I found this today on Engadget. http://translate.google.com/translate?u=ht...Flanguage_tools
    Im soo into Steampunk and the idea of "old" new technology. OK the laptop cant really  be used as a
    laptop cos I bet its not too happy to be bagged up and slung over your shoulder but as a decorative
    working object of art..its pretty amazing! I bet with out much hacking around with it you could have
    any make of model of laptop inside the shell. What do you think guys? Marky;)....
  22. Scrutinize My Chess Game!
    Check out my coding skillz! (6)
    OK, so here's the skinny: Back when AOL was THE place to be online (1998/1999), me and a few
    friends became internet vandals, using punters and things of that ilk to reek havoc in the
    chatrooms. Our heyday was short-lived, as punting became virtually impossible with AOL 4.0 and
    pretty much a thing of the past with later patches/releases. However, 'progs' -- as they
    were referred to, usually named by their creator with some variation of the words "Hacker," "Elite,"
    and "Toolz" -- did not die. Their golden age had passed, but souls desperate to keep them ....
  23. Collision Check Algorithm
    Seeing if two objects are overlapping (2)
    I have been looking around for a bit, and have not been able to find a solution to my problem. I was
    hoping someone here might know an algorithm for checking if two objects are overlapping. For
    example, I have two objects, each with an image. The objects have an x and y, but they have more
    than just a width and height - they have a shape. I'm not talking about rectangular collision,
    but actual (reasonably) precise measurements that don't take a huge amount of load or run-time
    processing. The best possible solution I could come up with would be to draw a blank o....
  24. Dell Laptop Batteries - Explode Into Flames
    Check your Dell note book battery!!!! (14)
    Dell plans to announce a recall of 4.1 million batteries worldwide. Dell has identified a potential
    issue associated with certain batteries sold with Dell Latitude™, Inspiron™, XPS™ and Dell Precision
    Mobile Workstation™ notebook computers. Here's a list of the affected models: Latitude: D410,
    D500, D505, D510, D520, D600, D610, D620, D800, D810 Inspiron: 6000, 8500, 8600, 9100, 9200, 9300,
    500m, 510m, 600m, 6400, E1505, 700m, 710m, 9400, E1705 Dell Precision: M20, M60, M70 and M90 mobile
    workstations XPS XPS, XPS Gen2, XPS M170 and XPS M1710 Have your batter....
  25. GreenBrowser - The Tiny No-Install Browser
    (9)
    I have tried this GreenBrowser several days. It is totally free to download , low memory cost and
    high performance. It supports AD filter,add others search Bar in it , add external toolbar and
    change skin. All useful functions within this small one. Besides function and performance, The
    software doesn't need to install .Just copy it and paste. Try it and you will like this small
    guy. ....
  26. Guitar Hero...check This One Out
    (11)
    Anyone play Guitar Hero for the PS2. Man that's an addicting game. I used to dabble with the
    guitar when I was younger, so this game is great. Its like playing the air guitar to well known
    songs, except you actually can hear yourself playing. I'm only playing on easy mode right now
    with the first three frets, and it's starting to get a little difficult. I suggest this game to
    anyone who dreamed of being in the limelight playing with a rock band but just couldn't hack it.
    It's just alot of fun!....
  27. How Do I Check What Graphics Card I Have?
    (25)
    I'm not sure what graphics card I have, it doesn't say on the maker's site so I need
    another way to find out, can anybody help me figure out what type of GFX card I am using?....
  28. Would You Like To Have A Spell Check For Your IPB?
    (2)
    Wouldn't it be nice to have a Spell Checker for your Invision board? Well I have one on my site
    and use it a lot mostly because the keys on my keyboard don't seem to be in the right place. I
    am certain that it must be defective, should my keyboard know what letter I want to type...I want a
    smart keyboard that knows what I am typing and it would be nice if it would read y mind and do it
    all, but till I can find a smart keyboard (I think they are hiding them from me!) I need a utility
    that will check what actually appeared on my monitor. You can have this also.....
  29. Very Addictive Ball Game
    Check this out (you need time!) (15)
    Just found this nifty game: Metaphysik II Warning: very addictive ! /wink.gif' border='0'
    style='vertical-align:middle' alt='wink.gif' /> You need lots of time ! John.....
  30. How To Check U'r Site Rank.
    (19)
    QUOTE Your Google rank is a position of your site's listing in pure, unpaid search results
    with a given keyword. E.g., if your rank is 8 with the keywords "widget", it means a serching person
    will see in in the 8th position in Google's search results. You can check your rankings in two
    ways: - by hand - using a ranking checking software. I wouldn't consider manual checks because
    they are way too consuming. So, your choice would probably go to a specialized software program like
    http://www.webceo.com What is good about Web CEO, it can check your rank....

    1. Looking for check, occurence, substring, string






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




Check For Occurence Of Substring In String