Jump to content

[SOLVED] select letters inbetween spaces and numbers


Recommended Posts

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!!!!!!!!

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. 

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.

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!

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.