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

inserting spaces in a string using regex 1

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
here are my strings...

string one: 'tubes8587'
string two: 'tubes1990'

and what i'd like to do is use preg_replace to insert spaces and/or hypens so i can do either of these two options..


tubes 85-87
tubes 1990


 
Code:
$pattern = '/([a-z]+?)(\d{2})(\d{2})/i';
preg_match($pattern, $input, $match);
//assuming $input contains the text to parse

the output will be as follows
Code:
$match[1] :the alpha stuff before the numbers
$match[2] :the first two numbers
$match[3] :the last two numbers.
you can thus reassemble the parts as you wish, with spaces, hyphens or happy faces.
 
so far i've tried this...

Code:
preg_match('#com\/(.*?).htm#', $page_urls[$i], $matched_category20);
$matched_category200 = $matched_category20[1];

// --------------------
$search03 = array (
		'#19#is'
		);

$replace03 = array (
		' 19'
		);

$matched_category2 = preg_replace($search03, $replace03, $matched_category200);

basic for seperating 'tubes1990' to 'tubes 1990', but really want i want to do is say put a space between letters and numbers

thats the main thing, then after i need logic for placing a hypen between 8587

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top