kevk3v Posted May 28, 2010 Share Posted May 28, 2010 This is an example of what i'm trying to do... <?php $text = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?'; //ok lets say i have this <- //i want to find the word in $text without having the word and then get it into a string //example: i have "h" and i want to find the word in $text that begins with "h" and get that word into a string ?> how do i do this?, sorry for my bad english btw... Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/ Share on other sites More sharing options...
ChemicalBliss Posted May 28, 2010 Share Posted May 28, 2010 If you know there there wont be randomly jumbled letters in the mix, then you can use a simple PCRE regular expression: <?php $string = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?'; // Get any string of 1 or more characters between a and z (with spaces). preg_match_all("/[a-z ]+/i",$string,$words); // echo results; foreach($words as $word){ echo($word."<br />"); } ?> -cb- Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1064742 Share on other sites More sharing options...
kevk3v Posted May 28, 2010 Author Share Posted May 28, 2010 wow thanks ok lets say i have some russian text example $string = "текст///вау:?23прохладный"; how can i get preg match to work with the russian text? you got exactly what i was looking for but what if i want to find text in russian ? Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1064776 Share on other sites More sharing options...
ignace Posted May 28, 2010 Share Posted May 28, 2010 If you know there there wont be randomly jumbled letters in the mix, then you can use a simple PCRE regular expression: <?php $string = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?'; // Get any string of 1 or more characters between a and z (with spaces). preg_match_all("/[a-z ]+/i",$string,$words); // echo results; foreach($words as $word){ echo($word."<br />"); } ?> -cb- Why bother writing all this as it's already built-in $text = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?'; print_r(str_word_count($text, 1)); ok lets say i have some russian text example $string = "текст///вау:?23прохладный"; The same applies: setlocale(LC_COLLATE, 'ru_RU'); $text = 'текст///вау:?23прохладный'; print_r(str_word_count($text, 1)); Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1064785 Share on other sites More sharing options...
kevk3v Posted May 28, 2010 Author Share Posted May 28, 2010 Thanks for showing me that function Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1064795 Share on other sites More sharing options...
kevk3v Posted May 28, 2010 Author Share Posted May 28, 2010 it's not working with the russian edit: works with setlocale(LC_ALL, 'ru_RU.cp1251'); can anything go wrong using this? Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1064804 Share on other sites More sharing options...
kevk3v Posted May 28, 2010 Author Share Posted May 28, 2010 Actually it doesn't work at all, it only displays english i want it to display russian only, help me please Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1064857 Share on other sites More sharing options...
ignace Posted May 29, 2010 Share Posted May 29, 2010 Actually it doesn't work at all, it only displays english i want it to display russian only, help me please What do you mean? Try setlocale (LC_ALL, array ('ru_RU.CP1251', 'rus_RUS.1251')); This will match words if you use a function like str_word_count() it will not translate them. Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1064944 Share on other sites More sharing options...
kevk3v Posted May 29, 2010 Author Share Posted May 29, 2010 Actually it doesn't work at all, it only displays english i want it to display russian only, help me please What do you mean? Try setlocale (LC_ALL, array ('ru_RU.CP1251', 'rus_RUS.1251')); This will match words if you use a function like str_word_count() it will not translate them. it's not translating example i use <?php setlocale(LC_ALL, array('ru_RU.CP1251', 'rus_RUS.1251')); $text = "тест,test"; print_r(str_word_count($text, 1)); ?> it shows "test" instead of the russian word and Ð¿Ñ€Ð¸Ð²ÐµÑ Quote Link to comment https://forums.phpfreaks.com/topic/203216-how-to-identify-a-word-and-get-it-into-a-string/#findComment-1065020 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.