Jump to content


Photo

detect first letter as a space


  • Please log in to reply
4 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 06 July 2006 - 01:52 PM

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?

<?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; 
<?php
if ($_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"/><?php
foreach ($output4 as $data) {
echo($data."\n");
}
?></textarea>
<input type="submit" name="submit" value="submit">
<?php
echo("<br><br> Add Commas<br>");
echo strtoupper(str_replace("\n",', ',$caps)); 
?>
</form>


if you need it explaining further just ask :) im in work for another hour...

Kind regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 July 2006 - 01:58 PM

Remove leading space:
$string = preg_replace('/^\s*/','', $string);

What are some examples of your input and output?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 06 July 2006 - 02:00 PM

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.

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 06 July 2006 - 02:22 PM

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 5AL

when pasted it was adding a space before the postcode

Chips 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 :)

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 July 2006 - 03:16 PM

got any good tutorials i can read thru for noob regex'rs??


Check my signature and the Regex forum.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users