supermerc Posted January 8, 2009 Share Posted January 8, 2009 Hey I have a variable $v[1] that either turns out as: Deposited item (#######) or: Awarded item (#######) to Name I want to separate those into different variables. For example if its Deposited I want item to be in separate variable alone and the ######## to be in another variable alone. If its awarded I want Item and ###### to be in variables alone but I also want Name to be in a variable alone as well. Can anyone help me? Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/ Share on other sites More sharing options...
.josh Posted January 8, 2009 Share Posted January 8, 2009 preg_match("~(?:Deposited|Awarded)\s([^\b]+?)\b\s\(([^\)]*)\)(?:\sto\b\s([^\b]+)\b)?~",$s,$match); echo "<pre>"; print_r($match); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732638 Share on other sites More sharing options...
.josh Posted January 8, 2009 Share Posted January 8, 2009 Actually I think this is probably a little better preg_match("~(?:Deposited|Awarded)\s([^\s]+)\s\(([^\)]+)\)(?:\sto\s([^\b]+))?~",$s,$match); echo "<pre>"; print_r($match); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732643 Share on other sites More sharing options...
supermerc Posted January 8, 2009 Author Share Posted January 8, 2009 all it does is echos Array ( ) Array ( ) A bunch of times :S Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732648 Share on other sites More sharing options...
.josh Posted January 8, 2009 Share Posted January 8, 2009 Really? Because when I do this: <?php $string[] = "Deposited item (#######)"; $string[] = "Awarded item (#######) to Name"; foreach ($string as $s) { preg_match("~(?:Deposited|Awarded)\s([^\s]+)\s\(([^\)]+)\)(?:\sto\s([^\b]+))?~",$s,$match); echo "<pre>"; print_r($match); echo "</pre>"; } ?> I get Array ( [0] => Deposited item (#######) [1] => item [2] => ####### ) Array ( [0] => Awarded item (#######) to Name [1] => item [2] => ####### [3] => Name ) Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732655 Share on other sites More sharing options...
supermerc Posted January 8, 2009 Author Share Posted January 8, 2009 Well how is it supposed to work if my variable isnt in there? ??? Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732657 Share on other sites More sharing options...
.josh Posted January 8, 2009 Share Posted January 8, 2009 what do you mean? put your variable in the place of $s Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732658 Share on other sites More sharing options...
supermerc Posted January 8, 2009 Author Share Posted January 8, 2009 I changed it to preg_match("~(?:Deposited|Awarded)\s([^\s]+)\s\(([^\)]+)\)(?:\sto\s([^\b]+))?~",$v[1],$match); echo "<pre>"; print_r($match); echo "</pre>"; and its still doing same thing Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732663 Share on other sites More sharing options...
.josh Posted January 8, 2009 Share Posted January 8, 2009 Well then $v[1] is not the string/format you posted. Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732665 Share on other sites More sharing options...
supermerc Posted January 8, 2009 Author Share Posted January 8, 2009 Well just before that bit of code you gave me I echoed out $v[1] and I get Deposited Surper Oil (7825939) Array ( ) Awarded Vampire Fangs (7786635) to ServeD Array ( ) Awarded Vampire Fangs (7786390) to ServeD Array ( ) Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732669 Share on other sites More sharing options...
.josh Posted January 8, 2009 Share Posted January 8, 2009 Okay so the problem is that you didn't specify that 'item' could be more than 1 word. You get what you ask for. preg_match("~(?:Deposited|Awarded)\s([^\(]+)\s\(([^\)]+)\)(?:\sto\s([^\b]+))?~",$s,$match); Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732684 Share on other sites More sharing options...
supermerc Posted January 8, 2009 Author Share Posted January 8, 2009 okay it works now will it still work if its 3 or 4 words? Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732688 Share on other sites More sharing options...
.josh Posted January 8, 2009 Share Posted January 8, 2009 yes. Link to comment https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/#findComment-732697 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.