siwelis Posted July 17, 2009 Share Posted July 17, 2009 select letters inbetween space and numbers I have a serious of forum entries that which need to isolate some info from... They all have this data Four Numbers, Spaces, Text, Numbers E.g. 1051 JoZlkA11215 Anyone know how to isolate that JoZlkA? Or can give me some direction? Thank you very much!!!!!!!! Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 17, 2009 Share Posted July 17, 2009 Example: $string = '1051 JoZlkA11215'; preg_match('#\d{4}s+([^\d]+)\d+#', $string, $text); echo $text[1]; // JoZlkA Quote Link to comment Share on other sites More sharing options...
ghostdog74 Posted July 18, 2009 Share Posted July 18, 2009 $string = '1051 JoZlkA11215'; $s = preg_replace("/[^a-zA-Z]+/","",$string); print $s; Quote Link to comment Share on other sites More sharing options...
.josh Posted July 18, 2009 Share Posted July 18, 2009 ghostdog74 your solution would be great if the user was looking to remove all letters from a string. OP: Hopefully nrg's solution will work for you, but I have a sneaking suspicion you're probably going to have to show the content that that is going to be pulled from. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 18, 2009 Share Posted July 18, 2009 OP: Hopefully nrg's solution will work for you... On that note, I just realized that I forgot to manually re-insert the backslash for the whitespace character class prior to post submission, so the pattern should be: #\d{4}\s+([^\d]+)\d+# and not what it currently is: #\d{4}s+([^\d]+)\d+# :: shakes head :: It's that posting issue again (previews removing backslashes for some bizarre reason). Sorry folks.. And yeah, without the OP displaying more of the circumstances, that solution may or may not work.. I can only go by what I'm given. Quote Link to comment Share on other sites More sharing options...
siwelis Posted July 20, 2009 Author Share Posted July 20, 2009 Thank you all for helping! NRG, I used your solution with the backslash. I figured it was something small that made it return a blank result, but I didn't know what it was. In the mean time, I wrote another method using str_ireplace for case insensitivity. $image = "4430 JoZlkA11215.jPg"; $numbers = array ('0','1','2','3','4','5','6','7','8','9','.JPG','.JPeG'); echo str_ireplace($numbers, '', $image).'<br />'; $string = '1051 JoZlkA11215.JpEg'; preg_match('#\d{4}\s+([^\d]+)\d+#', $string, $text); echo $text[1]; // JoZlkA I didn't mention that it would have .jpg (in up/lower case or jpeg), but I didn't think I would have to if it was coded in the manner you did; If I'm hypothesizing its operation properly, it says isolate from start of numbers to end of spaces, then from start of numbers to end of string. Is that right? Anyhow, I think your solution is better than mine (I think it's faster - I may test it when I get home, but in the mean time I'll be using your method). I also wanted to post mine here just to show a different way of doing it for others. Thank you again! Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 20, 2009 Share Posted July 20, 2009 If I'm hypothesizing its operation properly, it says isolate from start of numbers to end of spaces, then from start of numbers to end of string. Is that right? If you are referring to #\d{4}\s+([^\d]+)\d+#, not quite.. what this says is.. \d{4} - match 4 consecutive digits \s+ - followed by any whitespace character (one or more times) ([^\d]+) - then capture anything that is not a digit one or more times. This is the part that captures 'JoZlkA' \d+ - finally, follow that up by matching a digit or one or more times Anyhow, I think your solution is better than mine (I think it's faster - I may test it when I get home, but in the mean time I'll be using your method). Actually, string functionality like str_replace and friends are faster than regex. Regex is robust as hell, but comes at the cost of performance overhead. But pcre is still quite optimized, and truth be told, for small tasks such as this one, on a single pass, the speed difference between string functionality and pcre would be infinitesimal. Quote Link to comment 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.