preg_match() help, please

I have a data string that starts with a combination of upper/lower case letters, numbers, hyphen and period. The length of this part of the string is quite variable, do I’d like to use preg_match() to capture just this first portion of the string.
<code>
$string = ‘IN-A-103P-C Fly, C. S. Indian Camp; 1886 1886 C.S. Fly Photographer HP Photographs ina103pc.jpg Apache Indians’;

	if(preg_match("/^[-a-zA-Z0-9.]$/", $string, $matches)) {
	echo $matches[0];
	}

</code>

What I want the above code to return is IN-A-103P-C, stopping at that first whitespace, which is something besides a " ".

Please show me what I need to do to make the preg_match solution work.

Thanks!

Why not use explode if it will always be at the first portion of the string followed by a space?


$string = 'IN-A-103P-C Fly, C. S. Indian Camp; 1886 1886 C.S. Fly Photographer HP Photographs ina103pc.jpg Apache Indians';
$exploded_str = explode(' ', $string);
echo $exploded_str[0];

[QUOTE=centered effect;4955588]Why not use explode if it will always be at the first portion of the string followed by a space?

Tried that, but that space isn’t a " ", it’s some other kind of whitespace. Since I can’t figure out what kind of whitespace it is, it seems simple to do the pre_match() method. Then simply stop when a character is not A-Z, a-z, o-9, or -, or a period.

Just remove the $ at the end of your pattern. The $ means “the end of the match must be at the end of the string”. You don’t want this, so you can remove that. You should leave the ^ though, because you want the start of the match to be at the start of the subject string :slight_smile:

Tried that, but that space isn’t a " ", it’s some other kind of whitespace. Since I can’t figure out what kind of whitespace it is, it seems simple to do the pre_match() method. Then simply stop when a character is not A-Z, a-z, o-9, or -, or a period.[/QUOTE]

Sorry, I should have read that the first time. You could also replace the white space with a space as well, then explode it.
$string = preg_replace(“‘\s+’”, ’ ', $string);

Try this

$string = 'IN-A-103P-C Fly, C. S. Indian Camp; 1886 1886 C.S. Fly Photographer HP Photographs ina103pc.jpg Apache Indians';
list($code) = preg_split('#[^a-z0-9\\-]#si', $string, 2);
echo $code;

This works great, Gvre. Thank you!
Could you take a moment to explain in plain English what’s happening here?? Then I’ll have a better understanding for next time I run into a similar issue.

:slight_smile:

gvre’s script reads:

“Split the variable $string, using anything that isnt a-z,0-9,\, or - as a delimiter, and returning at most 2 parts (IE: Only do the first such split.).”

So what is the space character in this particular case then? How would the OP go about finding that out?

An easy way would be to show us the output of echo base64_encode($string). At least that way we can determine the actual contents, which would be a great start.

Thanks StarLion! Very helpful :slight_smile: