DiavolO Posted January 17, 2011 Share Posted January 17, 2011 First of all, I want to apologise for my incorrect english language Secondly with php i'm 'not very newbie', but there is enough space to improve... Also, I have problem with regular expresions: I have a string which is formed like: any word/(-s) ( any word/(-s) ) or any word/(-s) ( any word/(-s) ) ( any word/(-s) ) just like: Information (Technologies) or Speciality (Information Technologies) (High Grade) what i need is to shorten it with only first letters of each word, like: I (T) or S (IT) (HG) leaving all the brackets. Also, i have already managed with some code, which is not working correctly: $test3 = str_replace(" and ", " ", $subcat5); //replacing 'and' with space $test4 = explode(' ',$test3); foreach($test4 as $v){$test5.=substr($v,0,1);} $spec_trump = strtoupper($test5); //if needed so if string is: Speciality (Information Technologies) (High Grade) result will be: S(T(G what i need is: S (IT) (HG) Can anyone help me out, with this stuff? Quote Link to comment https://forums.phpfreaks.com/topic/224700-replacing-string-without-removing-brackets/ Share on other sites More sharing options...
JAY6390 Posted January 17, 2011 Share Posted January 17, 2011 $result = preg_replace('/(\b[a-z])[a-z]+\s?/i', '$1', $subject); Quote Link to comment https://forums.phpfreaks.com/topic/224700-replacing-string-without-removing-brackets/#findComment-1160633 Share on other sites More sharing options...
DiavolO Posted January 17, 2011 Author Share Posted January 17, 2011 Nice! Its working great! Could you explain '/(\b[a-z])[a-z]+\s?/i', '$1', this code piece? I'd like to learn and understand this. Quote Link to comment https://forums.phpfreaks.com/topic/224700-replacing-string-without-removing-brackets/#findComment-1160635 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.