[PHP] formatting a string

2004-04-24 Thread Andy B
hi...

i have a string i want to pull out of a database (mysql). the column name is
Phone1 and it is a 10 digit phone number. the raw string coming out of the
table column would look like this: 1234567890

what i want to do is format the string on display like this: (123)456-7890
but dont quite know how to start with that. what function(s) would i use for
that?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] formatting a string

2004-04-24 Thread Tom Rogers
Hi,

Sunday, April 25, 2004, 11:17:16 AM, you wrote:
AB hi...

AB i have a string i want to pull out of a database (mysql). the column name is
AB Phone1 and it is a 10 digit phone number. the raw string coming out of the
AB table column would look like this: 1234567890

AB what i want to do is format the string on display like this: (123)456-7890
AB but dont quite know how to start with that. what function(s) would i use for
AB that?


Try this:
$phone = '1234567890';
$newphone = preg_replace('/(\d{3})(\d{3})(\d)/','(\1)\2-\3',$phone);
echo $newphone.'br';

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] formatting a string

2004-04-24 Thread Andy B
[snip]
$phone = '1234567890';
$newphone = preg_replace('/(\d{3})(\d{3})(\d)/','(\1)\2-\3',$phone);
echo $newphone.'br';

--
regards,
Tom
[/snip]
ok sorry but since i never used preg_* before i dont quite get what some of
this stuff means. i looked at the doc page for it but it doesnt make mention
at all of what \d, \w, \s or any of those things mean... i only assume that
\d means digit and \w or \s means blank space??

anyways to go through the whole example above part by part:
$phone = '1234567890';//understand that
$newphone = preg_replace(//ok now what does this stuff
//mean??
'/(\d{3})(\d{3})(\d)/'
im gathering the line above is the search string (what to look for)? if so i
get from it that it is supposed to look for the first block of 3 digits then
the second block of 3 digits and the other 4 numbers by themself in a sense
seperating the string into 3 different parts: 123 456 7890 and then asigning
like id numbers to the blocks
'(\1)\2-\3'
and this one above says put block 1 between (). take block 2 and put a -
after it and leave the other 4 numbers alone to come up with: (123)456-7890
$phone);
the original number to do the replace on of course

let me know if i got that set right

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re[2]: [PHP] formatting a string

2004-04-24 Thread Tom Rogers
Hi,

Sunday, April 25, 2004, 3:15:25 PM, you wrote:

AB ok sorry but since i never used preg_* before i dont quite get what some of
AB this stuff means. i looked at the doc page for it but it doesnt make mention
AB at all of what \d, \w, \s or any of those things mean... i only assume that
AB \d means digit and \w or \s means blank space??

AB anyways to go through the whole example above part by part:
AB $phone = '1234567890';//understand that
AB $newphone = preg_replace(//ok now what does this stuff
AB //mean??
AB '/(\d{3})(\d{3})(\d)/'
AB im gathering the line above is the search string (what to look for)? if so i
AB get from it that it is supposed to look for the first block of 3 digits then
AB the second block of 3 digits and the other 4 numbers by themself in a sense
AB seperating the string into 3 different parts: 123 456 7890 and then asigning
AB like id numbers to the blocks
AB '(\1)\2-\3'
AB and this one above says put block 1 between (). take block 2 and put a -
AB after it and leave the other 4 numbers alone to come up with: (123)456-7890
AB $phone);
AB the original number to do the replace on of course

AB let me know if i got that set right

Yes that is exactly what it does .. sorry probably should have
explained it... but at least you read up the function :-)

the () tells preg to store the contents and they get numbered 1 2 3 ..
the \1 with a number below 10 tells it to use what it captured at that
point.

\D btw captures everything that is not a digit so you can use
that to cleanup the user input with $input = preg_replace('/\D/','',$input);

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Formatting a string for entry into MySQL

2003-09-23 Thread Burhan Khalid
James Johnson wrote:
Hi,

I'm trying to generate a string that contains a br, to insert into a MySQL
table. It appears the br is being stripped out either just before or
during the update.
http://www.php.net/mysql-escape-string

--
Burhan Khalid
phplist[at]meidomus[dot]com
http://www.meidomus.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] Formatting a string for entry into MySQL

2003-09-23 Thread James Johnson
Ok, thanks. It took a little bit of experimenting with printf(), sprintf()
and quotes, but I got it to work.

mysql_select_db($database_CCB, $CCB);
$ad_contact = [EMAIL PROTECTED]br909-555-1212;
$esAdContact = mysql_escape_string($ad_contact);
$q = sprintf(UPDATE subscriber_ads SET ad_contact = '%s' WHERE subid = 43,
$esAdContact);
$r = mysql_query($q, $CCB) or print(mysql_error()); 

Thanks,
James

-Original Message-
From: Burhan Khalid [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, September 23, 2003 4:45 AM
To: James Johnson; [EMAIL PROTECTED]
Subject: Re: [PHP] Formatting a string for entry into MySQL


James Johnson wrote:
 Hi,
 
 I'm trying to generate a string that contains a br, to insert into a 
 MySQL table. It appears the br is being stripped out either just 
 before or during the update.

http://www.php.net/mysql-escape-string

-- 
Burhan Khalid
phplist[at]meidomus[dot]com
http://www.meidomus.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Formatting a string for entry into MySQL

2003-09-22 Thread James Johnson
Hi,

I'm trying to generate a string that contains a br, to insert into a MySQL
table. It appears the br is being stripped out either just before or
during the update.

** code **
// has both info, update both
$ad_contact = $tr_email;
$ad_contact .= br;  
$pSep = -;
$ad_contact .= $tr_p1AC;
$ad_contact .= $pSep;
$ad_contact .= $tr_p1PRE;
$ad_contact .= $pSep;
$ad_contact .= $tr_p1SUF;

// update the record
$qUpdate = UPDATE subscriber_ads SET ad_contact = '$ad_contact' WHERE subid
= $sid; print($qUpdatebr); $rUpdate = mysql_query($qUpdate, $CCB) or
die(mysql_error());

The print() statement shows the correct format in the browser. I've looked
at printf() and sprintf(), but can't quite figure the syntax.

I want the br in the string so it will output correctly in the browser
when that data is retrieved from the DB.

Suggestions?

Thanks,
James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php