Welcome Guest, Not a member yet? Register   Sign In
Help with stripping value from return string
#1

[eluser]ppwalks[/eluser]
Hi all, got a bit of a problem and wondering if anyone knew of a way to tackle this problem, I have a search field to search specific data compiled of {letters - numbers - letters}, what i need to do is trim of the letters after the numbers, each record has a different string length which is why i cant just remove after a certain amount of characters.

Anyone ever done anything like this before or could point me in the right direction,

thanks all

Paul
#2

[eluser]johnpeace[/eluser]
That'll be a regular expression...which I suck at...but which there are plenty of people who love to show off their regexp skills...wait for it.
#3

[eluser]CroNiX[/eluser]
Is there a dash separating the 3 segments (letters-numbers-letters)? If so you could just explode() on the dash and only use the first 2 parts of the resulting array (letters-numbers)

Code:
$parts = explode('-', $the_string);
$term = $parts[0] . '-' . $parts[1];
#4

[eluser]kb1ibh[/eluser]
if there is no dash, and the solution above doesnt work, could you post some examples of these letter-number-letter combinations?


Edited to Add:
if there are no dashes, This might work... Havent had a chance to test it, but i dont think i made any serious errors

Code:
$string = YOUR_LETTER_NUMBER_LETTER_STRING;

preg_match("/([a-zA-Z]+)([0-9]+)([a-zA-Z]+)/", $string, $matches);

echo "part 1: " . $matches[0] . "\n";
echo "part 2: " . $matches[1] . "\n";
echo "part 3: " . $matches[2] . "\n";

It should work with both A1A and AAAAAA11111111AAAAAAAA, no matter how long the string is
#5

[eluser]ppwalks[/eluser]
thanks guys for the return messages, there are no dashes I demonstrated that to show the 3 seperate values, the values i'm using to search through are postal codes, in the database it has the postcodes as example: cg12 or b1 or can be b12 which is a different area completely so if I match the first letters and numbers it brings back the correct results.


hope that makes sense.

thanks

Paul
#6

[eluser]ppwalks[/eluser]
Unless there is an sql statement to do the same job also?
#7

[eluser]ppwalks[/eluser]
No doesn't work it takes first 3 characters




Theme © iAndrew 2016 - Forum software by © MyBB