DWilliams Posted January 18, 2010 Share Posted January 18, 2010 String processing and regex type things are my weakness. I think I'm looking for the opposite of sprintf. What I want to do is take an input string and assign parts of it to variables, or something similar that accomplishes the same goal. For example, something like this (obviously it won't work): $inputString = "Bob says to you, 'stuff' "; $pattern = "%0 says to you, '%1' "; magical_string_function($inputString, $pattern, $matchVar1, $matchVar2); echo $matchVar1; // Bob echo $matchVar2; // stuff Quote Link to comment https://forums.phpfreaks.com/topic/188842-how-can-i-match-parts-of-a-string-to-variables/ Share on other sites More sharing options...
premiso Posted January 18, 2010 Share Posted January 18, 2010 preg_match would be the way to do it: <?php $string = "Bob says to you 'hello'"; preg_match("~([A-Z0-9]+?) .* '([A-Z0-9]+?)'~i", $string, $matches); print_r($matches); ?> See my signature for references for Regular Expression Syntax. Quote Link to comment https://forums.phpfreaks.com/topic/188842-how-can-i-match-parts-of-a-string-to-variables/#findComment-996971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.