Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip out letters using PHP

Status
Not open for further replies.

weightinwildcat

Programmer
May 11, 2010
84
US
I am trying to tke letters out of some alphanumeric strings in a MySQL table. Right now I have code to query my table and write the results into a tab-delimited text file. The problem somes in when I try to use SUBSTR and other related functions to get rid of the letters.

Here is what part of a column might look like:

1000
W200
1300E
N1235S
N3N10200
I am trying to shave off any trailing letters or preceding letters. For those entries that look like N3W10200 I would like to be able to use Explode to get the numerical values and add them, but I need to be able to use the string functions effectively. Any thoughts as to what I am missing?

The output would look like this:

1000
200
1300
1235
10203

Have a great day.
 
Regular Expressions are your friend in this case.

Code:
$val="N1235S";
$pattern='/([A-Z])/'; 
$result=preg_replace($pattern,"",$val);
echo $result; [green]//1235[/green]

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
not sure how a regex would help with this one

N3N10200
becomes
10203

mind you, I'm not sure what would help with that - I can't see the logic that would allow us to establish a pattern.
 
In this case we are talking about addresses and house numbers, like one might find in some outlying areas.
 
I think what jpadie means is how or why does the 3 between then N's in the original alphanumeric string N3N10200, move to the position of the last zero for the result 10203.

In other words, wouldn't N3N10200 become [red]3[/red]10200 rather than 1020[red]3[/red]

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Not in this case; the idea is to have addition of the two numbers rather than concatenation. This needs to be done so I can merge the data set with another data set.

I have tried different things, like SUBSTR, RIGHT, and so on. So far, no luck. Not only do these commands not seem to work like I want, they do not seem to be doing ANYTHING in this situation. Very puzzling.
 
if you want more precise assistance please be more precise about the business rules you wish to implement.
 
Your code works really well. But I need to fine tune it.

What I need is to:

1 - Remove any leading letters (say, from N200W14500);
2 - Remove any trailing letters (say, from 1400SW);
3 - If there is a letter in the middle somewhere (200W14500, for instance), split the numbers (so I have would 200 and 14500), and then add them together (to get in this case 14700).

The business logic is hard to explain, but I am trying to merge two sets of data. There are equivalent fields in the sets for address ranges, but one uses letters and one does not.
 
O.k, how about this:

Code:
<?PHP
$val="N200W14500K";
$pattern='/([A-Z])/'; 
$result=preg_replace($pattern," ",$val);
echo $result . "<br>";
$sum=0;
$parts=explode(" ",$result);
foreach($parts as $part){
echo $part . "<br>";
$sum+=$part;
}

echo $sum;


This will replace any letters in your string with a space instead, upon which you can explode the numbers into an array, and then add them if there are more than one.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
this might work

Code:
<?php

$t = array(
'1000',
'W200',
'1300E',
'N1235S',
'N3N10200');
foreach($t as $item):
	echo "$item => ". weirdMatching($item) . "<br/>";
endforeach;


function weirdMatching($item){
	$pattern = '/^\D*(\d+)\D*(\d*)$/';
	preg_match($pattern, $item, $match);
	if ($match[2]):
		return $match[1] + $match[2];
	else:
		return $match[1];
	endif;
}
 
Thank you all for the feedback. I was able to adapt some of the code that was posted, and it works just fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top