How to find something in a string

Discussion in 'PHP' started by GeorgeBaker, Aug 17, 2012.

  1. #1
    Hey,

    Sorry i'm a little green in php as i have coded in asp classic before and it's really not the same :)

    I have e.g 3 strings containing the following text:

    $str1 = "is simply dummy text of the printing $$ 6/4r $$ and typesetting industry"
    $str2 = "is simply dummy text of the printing $$ 11/11tr $$ and typesetting industry"
    $str3 = "is simply dummy text of the printing $$ 15/6 $$ and typesetting industry"

    How can i get 6/4r, 11/11tr and 15/6 out in seperate variables?

    1. I think it something like search for '$$'
    2. Ask, is the next char a space?
    3. Ask, is the next char a number?
    4. Ask, is the next char a '/'

    When all these are true i want to grab e.g 6/4r out and put it in a separate var, but i don't know how to do it in php.

    Can anybody help me?

    Thanks!
     
    GeorgeBaker, Aug 17, 2012 IP
  2. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #2
    I think you can do it with regular expressions and the preg_match function

    preg_match
    http://php.net/manual/en/function.preg-match.php
    Code (markup):
    Regular Expression tutorial
    http://www.zytrax.com/tech/web/regex.htm
    Code (markup):
     
    GMF, Aug 17, 2012 IP
  3. GeorgeBaker

    GeorgeBaker Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks GMF, i have looked at php.net and the preg-match without luck, but let me try the other link at zytax.com

    Would be nice with some code example if possible.
     
    GeorgeBaker, Aug 17, 2012 IP
  4. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #4
    You have to use BOTH

    preg_match is a function that uses Regular expressions to search a string

    On php.net there are code examples :p

    Preg_match works as followed

    preg_match( ###SEARCH PATTERN###, ###STRING WHICH NEEDS TO SEARCHED###, ###ARRAY THAT SAFES THE MATCHES IF FOUND###);
     
    GMF, Aug 17, 2012 IP
  5. GeorgeBaker

    GeorgeBaker Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5

    Yes there is code, but it's useless for a beginners with all this '\\ //\\|?', you probably know what i mean :)

    Thanks for explaining the preg_match.

    I will try some coding now and get back later.
     
    GeorgeBaker, Aug 17, 2012 IP
  6. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #6
    Yes, I do know what you mean :D

    I would have posted the regex you needed, but I am not very good with it. But I think the regex you need is not that hard.
     
    GMF, Aug 17, 2012 IP
  7. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    The RegEx you need is something like this:
    /\$\$ ([^\$]+) \$\$/

    I can recommend the following website, when working with regular expressions: http://rubular.com/
     
    Soulstone, Aug 17, 2012 IP
  8. GeorgeBaker

    GeorgeBaker Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    GMF & Soulstone,

    This is how the code should look like and it works for me.

    Using regex:
    $str = 'is simply dummy text of the printing $$ 6/4r $$ and typesetting industr';
    preg_match('|\$\$(.*)\$\$|',$str,$match);
    echo $match[1];
    
    PHP:
    Using explode:
    $pieces = explode(' $$ ', $str);
    //Should contain 6/4r
    echo $pieces[1]
    PHP:
    Using preg-match as a function:
    function getValue($str){
        $pattern = '/\$\$\s*([^\$\s]+)\s*\$\$/i';
        if(preg_match($pattern, $str, $match)){
            return $match[1];
        }
        return false;
    }
     
    echo getValue($str);
    PHP:
    Soulstone! If you can, please explain to me and others how all this crab code works :) /\$\$\s*([^\$\s]+)\s*\$\$/i

    I might have another problem though, but i'll get back to that later.

    Thanks for your help!
     
    GeorgeBaker, Aug 17, 2012 IP
  9. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    If you use preg_match_all, you can match multiple results in a string.
    The regular expression needs a "boundary" to begin with, I use /. So the regex must start and end with /.
    Backslash is an "escape" character. $ has a specific meaning in regular expressions, so to actually use the character $ you need to escape it with \ infront.
    Everything between parentheses will be "captured" in the resulting array.
    Square brackets indicate the characters allowed, or disallowed if you have [^ ] as the case I wrote.
    After the last slash, you put an 'i' which means the match should be case-insensitive. It doesn't really make any difference in your case though, since you're not trying to match any letters.

    Was that the kind of explaination you needed?
     
    Soulstone, Aug 17, 2012 IP
  10. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    Try doing something like this:

    
    <?php
    $str = 'is simply dummy text of the printing $$ 6/4r $$ and typesetting industry is simply dummy text of the printing $$ 11/11tr $$ and typesetting industry';
    $pattern = '/\$\$ ([^\$]+) \$\$/i';
    preg_match_all($pattern, $str, $match);
    
    
    echo '<pre>'.print_r($match, true).'</pre>';
    ?>
    
    PHP:
    Should give you some answers about what it does.
     
    Soulstone, Aug 17, 2012 IP
  11. GeorgeBaker

    GeorgeBaker Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yes i see how it works. I will re-read your comment later again to see if i understand.
     
    GeorgeBaker, Aug 17, 2012 IP
  12. GeorgeBaker

    GeorgeBaker Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Yeah sort of, but i don't understand the escape thing.

    Escape from what?

    If this is disallowed [^ ] then i don't understand the $. This would mean that $ sign are disallowed or what? ([^\$]+) and what about the '+' sign?
     
    GeorgeBaker, Aug 17, 2012 IP
  13. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    Escape from the actual "meaning". $ normally means the end of a string in regex. It's a has a specific function. If you want the actual character, you need to "escape it" from the normal meaning.
    [^\$]+ means "at least one character that is not $". The + sign means "one or more of" and [^$] means "everything but $". It could have been [^abc] which means "everything but a, b and c".
    The point of it is to allow anything between the two sections of $$, so you can have "$$ anything here except dollar-sign $$", and it would be a match.
    You do this, because else the result of "$$ something $$ other things $$ something $$" with .+ instead of [^\$]+ would be the exact string, since it would match the last occurence of $$.
     
    Soulstone, Aug 17, 2012 IP
  14. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #14
    I suggest you should look at http://rubular.com/ at the bottom, there's a quick-reference at the bottom, that will show some of the built-in "functions" in Regular Expressions. Example . (dot) means "any single character". So if you do .+ it means "at least one of any single character".
     
    Soulstone, Aug 17, 2012 IP
  15. GeorgeBaker

    GeorgeBaker Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thank you Soulstone for the explanation and i will take a look at that site. Thanks!
     
    GeorgeBaker, Aug 17, 2012 IP
  16. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #16
    You're welcome! There's tons of tutorials etc. online if you google for it. Regular Expressions are very powerful and can be used for a lot of things.
     
    Soulstone, Aug 17, 2012 IP
  17. vaayaaedu

    vaayaaedu Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    using substring or regular expression. Two ways to accomplice the task.
     
    vaayaaedu, Aug 17, 2012 IP
  18. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #18
    substr() does not match a pattern, it truncates a string to a given length.
     
    BRUm, Aug 18, 2012 IP
  19. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #19
    You can use substr in a combination with strpos though.
     
    Soulstone, Aug 18, 2012 IP
  20. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #20
    I know you could, but why would you take the long way around? Also, he made no mention of strpos().
     
    BRUm, Aug 18, 2012 IP