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!
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):
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.
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 Preg_match works as followed preg_match( ###SEARCH PATTERN###, ###STRING WHICH NEEDS TO SEARCHED###, ###ARRAY THAT SAFES THE MATCHES IF FOUND###);
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.
Yes, I do know what you mean 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.
The RegEx you need is something like this: /\$\$ ([^\$]+) \$\$/ I can recommend the following website, when working with regular expressions: http://rubular.com/
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!
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?
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.
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?
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 $$.
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".
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.