shocker-z Posted July 6, 2006 Share Posted July 6, 2006 Ok guys, here's my code.. by far not the fastest way but it's what i've peiced together so then i can enter an address from work and will format it in caps and to paste directly into my job booking system (very very very hand beleave me!) but what i've found is some addresses contain , , and what i have found is my code below will put a space before the next line i was wonder what the best way is to take a var and replace any spaces that exist before a letter or number?[code]<?php $caps=$_POST['caps'];$caps=str_replace(' ',' ',$caps);$caps=str_replace(', ,',', ',$caps);echo("Full format..<br>");$output=stripslashes(strtoupper($caps));$output=str_replace(', ',"\n", $output);$output2=explode("\n",$output);$i=0;foreach ($output2 as $data) {$i++;if ($i==1) {$output3=substr($data, 0, 24);}if ($i==2) {$output3=substr($data, 0, 24);}if ($i==3) {$output3=substr($data, 0, 14);}if ($i==4) {$output3=substr($data, 0, 14);}if (($i!==1) && ($i!==2) && ($i!==3) && ($i!==4)) {$output3=substr($data, 0, 24);}$output4[]=$output3;}?><style>.transform { text-transform: uppercase; <?phpif ($_POST['caps']) { if (count($output4) !== 4) { echo('background-color: #DAA520;'); } else { echo('background-color: #90EE90;'); }}?>}</style><form name="form1" method="post" action=""><textarea id="caps"name="caps" class="transform" cols="30" rows="10"/><?phpforeach ($output4 as $data) {echo($data."\n");}?></textarea><input type="submit" name="submit" value="submit"><?phpecho("<br><br> Add Commas<br>");echo strtoupper(str_replace("\n",', ',$caps)); ?></form>[/code]if you need it explaining further just ask :) im in work for another hour...Kind regardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/13842-detect-first-letter-as-a-space/ Share on other sites More sharing options...
effigy Posted July 6, 2006 Share Posted July 6, 2006 Remove leading space:[code]$string = preg_replace('/^\s*/','', $string);[/code]What are some examples of your input and output? Quote Link to comment https://forums.phpfreaks.com/topic/13842-detect-first-letter-as-a-space/#findComment-53860 Share on other sites More sharing options...
Chips Posted July 6, 2006 Share Posted July 6, 2006 My first thought was just to explode it by the commas, and then you have an array - natural "line breaks" by the individual elements, white space can easily be trimmed too. Quote Link to comment https://forums.phpfreaks.com/topic/13842-detect-first-letter-as-a-space/#findComment-53862 Share on other sites More sharing options...
shocker-z Posted July 6, 2006 Author Share Posted July 6, 2006 effigy that worked a treat mate :) i was litraly just reading on regex as it's somthing i've never got the gist of.. got any good tutorials i can read thru for noob regex'rs??sample data would be: Ormskirk Road, Aintree, LIVERPOOL, , L9 5ALwhen pasted it was adding a space before the postcodeChips it doesn't just do comma seperated it also formats an address that is in seperate lines and shortens the lines to fit in perfect and turns it all into caps.Thanks for help guys :)RegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/13842-detect-first-letter-as-a-space/#findComment-53881 Share on other sites More sharing options...
effigy Posted July 6, 2006 Share Posted July 6, 2006 [quote author=shocker-z link=topic=99653.msg392533#msg392533 date=1152195774]got any good tutorials i can read thru for noob regex'rs??[/quote]Check my signature and the Regex forum. Quote Link to comment https://forums.phpfreaks.com/topic/13842-detect-first-letter-as-a-space/#findComment-53905 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.