Jeffro Posted June 11, 2011 Share Posted June 11, 2011 How would you grab the 2nd, 3rd and 4th characters in a string that always immediately followed .com (keeping in mind they would always be different characters)? Quote Link to comment https://forums.phpfreaks.com/topic/239102-how-would-you-achieve-this/ Share on other sites More sharing options...
fugix Posted June 11, 2011 Share Posted June 11, 2011 you could use regular expressions to achieve this preg_match('/\.com(.{3})/', $string, $matches); echo $matches[1]; Edit: forgot to mention, your $string variable would be the URL Quote Link to comment https://forums.phpfreaks.com/topic/239102-how-would-you-achieve-this/#findComment-1228497 Share on other sites More sharing options...
cunoodle2 Posted June 11, 2011 Share Posted June 11, 2011 Well first thing that you need to do is to get the location of the ".com" in the string. You would do that like this.. <?php $pos = strpos($string, '.com', 1); ?> From there you could use the substring function to get the remaining letters like this.. <?php //right now the value of "$pos" is beginning of the '.com' //we need to increase it by 5 to adjust for the length of the .com as well as scrapping the 1st letter like you requested $pos = $pos +5; $result = substr($string, $pos, 3); echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/239102-how-would-you-achieve-this/#findComment-1228498 Share on other sites More sharing options...
Jeffro Posted June 11, 2011 Author Share Posted June 11, 2011 Thx guys. I combined both of your solutions to make it work. Since the first example provided the first 3 characters only and I needed 2, 3, and 4, I used it.. and then used strpos to get what I needed from that. Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/239102-how-would-you-achieve-this/#findComment-1228501 Share on other sites More sharing options...
fugix Posted June 11, 2011 Share Posted June 11, 2011 i misread your post, so if you wanted to use my code i will revise to be exactly what you want, the second third and fourth characters after the .com, im assuming that you do not want the forward slash preg_match('/\.com\/(.{3})/', $string, $matches); echo $matches[1]; Quote Link to comment https://forums.phpfreaks.com/topic/239102-how-would-you-achieve-this/#findComment-1228504 Share on other sites More sharing options...
Jeffro Posted June 11, 2011 Author Share Posted June 11, 2011 i misread your post, so if you wanted to use my code i will revise to be exactly what you want, the second third and fourth characters after the .com, im assuming that you do not want the forward slash preg_match('/\.com\/(.{3})/', $string, $matches); echo $matches[1]; Cool.. even easier. Thanks for the help. Much appreciated! Works great. Quote Link to comment https://forums.phpfreaks.com/topic/239102-how-would-you-achieve-this/#findComment-1228506 Share on other sites More sharing options...
fugix Posted June 11, 2011 Share Posted June 11, 2011 i misread your post, so if you wanted to use my code i will revise to be exactly what you want, the second third and fourth characters after the .com, im assuming that you do not want the forward slash preg_match('/\.com\/(.{3})/', $string, $matches); echo $matches[1]; Cool.. even easier. Thanks for the help. Much appreciated! Works great. no problem at all, glad it worked for you Quote Link to comment https://forums.phpfreaks.com/topic/239102-how-would-you-achieve-this/#findComment-1228507 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.