Jump to content

Archived

This topic is now archived and is closed to further replies.

shocker-z

detect first letter as a space

Recommended Posts

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;
<?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>[/code]


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

Kind regards
Liam

Share this post


Link to post
Share on other sites
Remove leading space:
[code]$string = preg_replace('/^\s*/','', $string);[/code]

What are some examples of your input and output?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.