applewilliam Posted December 20, 2009 Share Posted December 20, 2009 Hello, I have a string that looks like this : hj45ederl840lmcjower oldwsd3ohwxrzswlzswd I want to extract every 3 characters (even if it is a space). So it should read: hello world. any advice? Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/ Share on other sites More sharing options...
ChemicalBliss Posted December 20, 2009 Share Posted December 20, 2009 The easiest method is a recurring substr function: use a while() loop eg: <?php $cstring = "hj45ederl840lmcjower oldwsd3ohwxrzswlzswd"; $new_string = substr($cstring,0,1); // Gets the first letter. $cstring = substr($cstring,1); // Removes First letter from current string. while($more_left == true){ // While there is more to do, continue the loop. // If there is at least 4 characters left (3 to skips and 1 to add), then do this. if(strlen($cstring) >= 4){ $new_string .= substr($cstring,3,1); // Add the fourth letter (skip 3 letters) $cstring = substr($cstring,4); // Get rid of that last part so we can continue with the next letter. }else{ $more_left = false; // If there arent enough characters to skip then there must be no characters to save, so break the loop. } } echo($new_string); // Untested. ?> That should work. substr is extremely useful. Hope this Helps, -CB- Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/#findComment-980968 Share on other sites More sharing options...
Buddski Posted December 20, 2009 Share Posted December 20, 2009 Its not perfect but it works $str = 'hj45ederl840lmcjower oldwsd3ohwxrzswlzswd'; for($i=0;$i<strlen($str);$i+=4) { echo $str{$i}; } Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/#findComment-980970 Share on other sites More sharing options...
applewilliam Posted December 20, 2009 Author Share Posted December 20, 2009 Its not perfect but it works $str = 'hj45ederl840lmcjower oldwsd3ohwxrzswlzswd'; for($i=0;$i<strlen($str);$i+=4) { echo $str{$i}; } Ok if the string needs to be extracted every 2 letters. How do I make a variable for this $i+=4. That can be set Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/#findComment-980974 Share on other sites More sharing options...
ChemicalBliss Posted December 20, 2009 Share Posted December 20, 2009 You change the number 4 into a variable, and set it earlier in the script. Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/#findComment-980975 Share on other sites More sharing options...
applewilliam Posted December 20, 2009 Author Share Posted December 20, 2009 You change the number 4 into a variable, and set it earlier in the script. Yea i got it thanks Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/#findComment-980976 Share on other sites More sharing options...
.josh Posted December 20, 2009 Share Posted December 20, 2009 $string = "hj45ederl840lmcjower oldwsd3ohwxrzswlzswd"; preg_match_all('~(.)(.{3})?~',$string,$chars); // example output echo "<pre>"; print_r($chars[1]); Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/#findComment-980977 Share on other sites More sharing options...
printf Posted December 20, 2009 Share Posted December 20, 2009 Using preg_replace, It returns only want you want. <?php $text = 'hj45ederl840lmcjower oldwsd3ohwxrzswlzswd'; echo preg_replace ( "~(.){1}.{3}~", "\\1", $text ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/185780-extracting-from-string/#findComment-980982 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.