Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/13842-detect-first-letter-as-a-space/
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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.