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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex help, using limiters 1

Status
Not open for further replies.

kaptlid

Technical User
Nov 26, 2006
86
US
Having a problem preserving words.
input:
$string = 21st street 1st street;
or
$string = 21st st 1st st;

$patterns[0] = '[(?<=[0-9])st|nd|rd|th]';
$patterns[1] = '/ave.*?\b/';
$patterns[2] = '' //confused...
$replacements[0] = "";
$replacements[1] = "ave";
$replacements[2] = "st"
$str = preg_replace($patterns, $replacements, $string);

How can I tell it to preserve the first two letters of street and the abbreviation st if it doesn't have a number right behind it?

The output should end up looking like
21 st 1 st
 
I would approximate the desired functionality by something like this.
[tt]
$pattern="/\b(\d{1,2})((st|nd|rd|th)?)(\s+)((st)(\.|reet\b)?|(ave)(\.|nue\b)?)/i";
$replace="$1 $6$8";
[/tt]
 
$replace="$1 $6$8";

What do the dollar signs and numbers refer to?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top