gerkintrigg Posted January 4, 2008 Share Posted January 4, 2008 Hello. I'm trying to work out how to get certain words from a text string into an array. Essentially, I want to take all words beginning with a $ symbol from an HTML file and bung them into an array for processing. This way, I can have people upload HTML files and my PHP project can replace them with other stuff (like names, input boxes etc). I know the explode command and could loop through to split at the $ symbol, then loop through to break at the following space character, but it seems a long-winded way of doing it. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/84448-words-in-a-string/ Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 consider this <?php $strTest = '$this is $best a test'; preg_match_all("~\\\$([\w\d]+)?~",$strTest,$arrMatches); print_r($arrMatches[0]); ?> hope its helpfull... Quote Link to comment https://forums.phpfreaks.com/topic/84448-words-in-a-string/#findComment-430223 Share on other sites More sharing options...
gerkintrigg Posted January 4, 2008 Author Share Posted January 4, 2008 I think i understand the premise, but I don't want to print the words, but put them into an array and as I'm not familiar with your way of matching the string (preg_match always seems so impenetrable), I'm not sure how to do this. I tried using a foreach loop: <?php $strTest = '$this is $best a test'; preg_match_all("~\\\$([\w\d]+)?~",$strTest,$arrMatches); print_r($arrMatches[0]); foreach ($arrMatches[0] as $key => $value){ echo $value[$key]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84448-words-in-a-string/#findComment-430301 Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 well for your first answer the code should be <?php $strTest = '$this is $best a test'; preg_match_all("~\\\$([\w\d]+)?~",$strTest,$arrMatches); print_r($arrMatches[0]); foreach ($arrMatches[0] as $key => $value){ echo $value; } ?> better still consider this following code <?php $arrData = array("username"=>"rajivgonsalves","email"=>"rajivgonsalves@yahoo.com"); $strTest = 'Your username is $username and your email is $email'; $strTest = preg_replace("~\\\$([\w\d]+)?~e","\$arrData['$1']",$strTest); echo $strTest; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84448-words-in-a-string/#findComment-430303 Share on other sites More sharing options...
gerkintrigg Posted January 4, 2008 Author Share Posted January 4, 2008 Hi. Thanks for that. I got this to work: $strTest = '$this is $best a test'; preg_match_all("~\\\$([\w\d]+)?~",$strTest,$arrMatches); $my_array =$arrMatches[0]; foreach ($my_array as $key => $value){ echo $value; } for some reason, using an array delimiter doesn't seem to work in a foreach loop eg: foreach ($arrMatches[0] as $key => $value){ so i needed to put the array into a different variable. but thanks for the details - It's very useful. Quote Link to comment https://forums.phpfreaks.com/topic/84448-words-in-a-string/#findComment-430315 Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 actually that should work without any problem Quote Link to comment https://forums.phpfreaks.com/topic/84448-words-in-a-string/#findComment-430319 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.