PHP - Formating A Numeric String
Looking for ideas and advise on this one. I want to take a 10 digit number and 1234567890 and format it like 132-456-7890 what would be my best bet as choice of methods to do this?
Similar TutorialsIs there an easy way to find a numeric value in a string if I don't know what the string will contain? For example I want '52' out of: mystring52 Hopefully there is already a simple function to do this? I'm thinking there may not be as the string having more than one group of numbers may confuse matters. I'm just dealing with abcdef123 or similar in this case though. Hi, I am trying to convert a String date into numeric date using PHP function's, but haven't found such function. Had a look at date(), strtotime(), getdate(); e.g. Apr 1 2011 -> 04-01-2011 Could someone please shed some light on this? Regards, Abhishek I'm using system() to run NMAP and then display the output but it's one big string, how can I format it to look like what I see in the command line? web app output: Starting Nmap 5.00 ( http://nmap.org ) at 2012-03-21 18:44 CDT Interesting ports on localhost (127.0.0.1): Not shown: 993 closed ports PORT STATE SERVICE 80/tcp open http 111/tcp open rpcbind 139/tcp open netbios-ssn 443/tcp open https 445/tcp open microsoft-ds 631/tcp open ipp 3306/tcp open mysql Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds 0 command line output: Starting Nmap 5.00 ( http://nmap.org ) at 2012-03-21 18:48 CDT Interesting ports on localhost (127.0.0.1): Not shown: 993 closed ports PORT STATE SERVICE 80/tcp open http 111/tcp open rpcbind 139/tcp open netbios-ssn 443/tcp open https 445/tcp open microsoft-ds 631/tcp open ipp 3306/tcp open mysql Nmap done: 1 IP address (1 host up) scanned in 0.45 seconds Code: [Select] <?php // path to NMAP $Path = '/usr/bin/nmap'; if(isset($_POST['scan'])) { $IP = $_POST['IP']; $ScanType = $_POST['ScanType']; echo "<br /><u>Results for NMAP Scan</u> <br />"; system("$Path $ScanType $IP 2>&1", $Output); echo $Output; } ?> any help would be greatly appreciated, thanks in advance!! I'm trying to figure out how to format a variable. I am listing the contents of a directory from an array and I trying to format the date field to be like - Jan 07, 2011 01:35 PM. When I try this format I get a 500 error message. <td width="'.$width_of_dates_column.'">'.$files("M d Y h:i:s A", filemtime['date'])[$key].'</td></tr>'; Is is possible to format the date after it's been put into the array or does it have to be done while it's being processed. Not sure if you need more code to determine the formatting. Please let me know if you do. Thank you. Is there any way to disable the "A non-numeric value encountered" warning in the php.ini file? I don't get that warning on my Apache server but I do when I use the same code on my web host's server. I never got the warning until I uploaded a two line php.ini file to my web host so that I could keep my sessions longer than the default 24 minutes. This is the same online program related to my earlier post about too many inputs. I wanted to try sessions for a 24 hr. period but I'm getting tons of errors for any field that is not used which can be in the hundreds. If I delete my php.ini file from my web host & just let things default to whatever they are using then I don't get those errors, I use a simple machines forum and want to combine a table I have and a smf table and I need the dates in a numberic format(like smf uses). Sooo ... how do I get the time in a format like this 1336392447 Thanks Hi Folks, I understand the basic math to accomplish what I want, at least I think I do I would like to confirm my logic and I need help with the final output format. I am coding a simple browser based, resource type of game. The gamer will select a certain item to produce another item and it will take x time to do it. Here is the basics of what I want, minus the coding to make it look pretty. How do I format $y to show HHH:MM:SS? Code: [Select] $complete = time() + $x; // To show how much time is remaining until completion or complete $y = $complete - time(); if $y > 0 { echo $y; } else { echo "complete"; } I have created an email template for HTML emails, but something isn't quite right. I use roundcube for my webmail and the email shows up perfect, but then in outlook it looks terrible. Gmail wont even receive it and in godaddy's webmail it is displayed at text with the <table> code written out. Any idea what I am missing? // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: <[mail]>' . "\r\n"; $headers .= 'From: cjkjckjxc <noreply@cojch.com>' . "\r\n"; $body = <<<BODY //stuff goes here BODY; Hi All, not able to understand I'm making miskate in CSS or PHP foreach statement. Need help with proper formating of the below code. I want to format the so that seasons divs will contain all the details about the episodes, title & plot. As of now season div is only wraping around the first episode only & ignoring remaining episodes details. Sorry I'm not able to explain this in words but if you access below code you will be able to understand. Code: [Select] <html> <head> <style type="text/css"> .main { width: 750px; margin-right: auto; margin-left: auto; background: #cc1; clear: both; overflow:auto; } .season { margin: 5px; padding: 5px; width: 730px; background: #960; } .details { overflow:hidden; } .ep, .title, .airdate, .plot { margin: 5px; padding: 5px; width: 675px; background: #CCC; } </style> </head> <body> <?php $eps = array( array( 'year' => '2005', 'eptitle' => 'title1', 'airdate' => 'Apr 24 2009', 'epplot' => 'plot of S01E01', 'season' => '1', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title2', 'airdate' => 'May 1 2009', 'epplot' => 'plot of S01E02', 'season' => '1', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title3', 'airdate' => 'May 8 2009', 'epplot' => 'plot of S02E01', 'season' => '2', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title4', 'airdate' => 'May 12 2009', 'epplot' => 'plot of S02E02', 'season' => '2', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title5', 'airdate' => 'May 15 2009', 'epplot' => 'plot of S03E01', 'season' => '3', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title6', 'airdate' => 'May 28 2009', 'epplot' => 'plot of S03E02', 'season' => '3', 'episode' => '2'), ); //var_dump($eps); $season = 0; echo "<div class='main'>"; foreach($eps as $k){ // season check start if($season!=$k['season']) { echo "<div class='season'><b>SEASON : </b>".$k['season']; } //season check finish echo "<div class='details'>"; echo "<div class='ep'><b>EPISODE : </b>".$k['episode']."</div>"; echo "<div class='title'><b>TITLE : </b>".$k['eptitle']."</div>"; echo "<div class='airdate'><b>AIRDATE : </b>".$k['airdate']."</div>"; echo "<div class='plot'><b>PLOT : </b>".$k['epplot']."</div>"; echo "</div>"; // season check start if($season!=$k['season']) { echo "</div>"; $season = $k['season']; } } echo "</div>"; ?> </body> </html> So basically I have text which appears on an image. Code: [Select] $ability = "$title gets ability"; //Create Ability imagettfbbox(30, 0, $font, $ability); imagettftext($image, 30, 0, 70, 700, $fontcolor, $font, stripslashes($ability)); But the line of text is longer than the width of the area on the image it needs to fit in. How can I change this so it automatically starts a new line once it reaches the end of said area? Hi! I'm working a on wordpress website with member pages. Members enter their information via custom fields. Everything is working great except when trying to create a link based on one of those custom field. Unless the member enter his website address in the http://mysite.com format the link my code create is not valid. This is the code i'm currently using Code: [Select] <?php $msite = get_post_meta($post->ID, 'ccio_site', true) ; if (!empty($msite)) { ?><li>Site Internet: <a href="<?php echo ($msite) ; ?>"><?php echo ($msite) ; ?></a></li><?php } ?> Example of a member page where the format create an error. The input field is: Code: [Select] www.pechabot.com Example of a member page where the format is ok. The input field is Code: [Select] http://www.annuairesgb.com How can I format the input string so my code will work even if someone doesn't write the full address with the http:? Thanks a lot for looking Hi, I have a page that lists the names of the Artists in one column, and the title of their song in the next column. I've included a sort section that allows users to view the artist (with the corresponding title) that starts with whatever letter they press. I've now come to a point where I need to add in a check for numbers. (View attached image to understand what we're talking about) So if the artist name starts with any number between 0-9, it will be shown in order on the page once a user clicks on "#". I've tried doing it various ways, but can't seem to get it just right. Here is the code (I took out all my attempts to get the numeric sort working): <?php session_start(); include_once('inc/connect.php'); if (isset($_SESSION['username'])){ $loginstatus = "logout"; } else{ $loginstatus = "login"; } if(!isset($_SESSION['sort_counter'])) {$_SESSION['sort_counter'] = 1;} if(($_SESSION['sort_counter']%2) == 0){ //test even value $sortcount = "DESC"; }else{ //odd value $sortcount = ""; } $result = mysql_query("SELECT * FROM sheets ORDER BY artist"); $sheetscount = mysql_num_rows($result); $sortletteris = $_GET['letter']; $downloadclick = $_GET['downloadclick']; $show = $_GET['show']; $today = date("Y-m-d"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" type="text/css" href="styles/style.css" /> </head> <body bgcolor="#343331"> <!-- Header --> <div id="header"> <div id="headerleft"></div> <div id="headermiddle"><a href="index.php"><img src="img/logo.png"></a></div> <div id="headerright"> </div> </div> <!-- Content Top --> <div id="contenttop"> <div id="links"> <!-- 92x30 --> </div> </div> <!-- Content Middle --> <div id="contentmiddle"> <div id="content"> <div id="sort"> <?php echo "<center>".$sheetscount." Sheets Available<br />"; echo "<a href='newlyadded.php'>New Sheets</a><span> | </span><a href='request.php'>Request a Sheet</a></center>"; $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; echo "<center><div id='letters'>"; $i = 0; while ($i<26){ $sortletter = $letters[$i]; echo "<a href='index.php?letter=".$i."'>".$letters[$i]." </a>"; $i += 1; } echo " <a href='index.php'>All</a></div></center>"; if (($sortletteris)!=""){ // The letter that was clicked is set to this variable $mysortletter = $letters[$sortletteris]; //echo $mysortletter; // lname LIKE '$letter%' $result = mysql_query("SELECT * FROM sheets WHERE artist REGEXP '^[$mysortletter]' ORDER BY artist $sortcount"); $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run } elseif (($sortletteris)==""){ $result = mysql_query("SELECT * FROM sheets ORDER BY artist $sortcount"); $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run } $greenboxleft = "greenboxleft"; $greenboxright = "greenboxright"; $grayboxleft = "grayboxleft"; $grayboxright = "grayboxright"; $colorvalue = 0; echo "<br /><table width='600px' align='center' style='border-collapse:separate; border-spacing:0px;'><th style='background-color: #cccccc; border-bottom-style: solid; border-color: #6aa504;'>Artist</th><th style='background-color: #cccccc; border-bottom-style: solid; border-color: #6aa504;'>Title</th>"; while($row = mysql_fetch_array($result)) { if(($colorvalue%2)==0){ $styleleft = $greenboxleft; $styleright = $greenboxright; } else{ $styleleft = $grayboxleft; $styleright = $grayboxright; } echo "<tr>"; echo "<td align='center' width='250' id='$styleleft'><div id='songsboxleft'>". ucwords($row['artist']). "</div></td>"; echo "<td align='center' width='250' id='$styleright'><div id='songsboxright'><a target='_blank' name='downloadclick' href='".$row['url']."'>" .ucwords($row['title']). "</a></div></td>"; echo "</tr>"; $colorvalue++; } echo "</table>"; ?> </div> </div> </div> <!-- Content Bottom --> <div id="contentbottom"> </div> </body> </html> Hello i would like to as a little help on how to proper format an foreach output into an HTML table. I have a small function inside an class, that will retrieve the data of an MySQL table and return the result in 1 array, Outside that function i can get the values displayed i only need to format it to show in an table here's what i have: function that gets the data Code: [Select] function getfansub(){ $subfanarray = array(); $query = mysql_query("SELECT * FROM anr_fansub WHERE 1"); while($result = mysql_fetch_array($query)){ $subfanarray[] = $result; } return $subfanarray; } Outside the funtion/class Code: [Select] $fansubarray = $mysql->getfansub(); VAR_DUMP( $fansubarray ); echo "<br><br>"; foreach($fansubarray as $name){ echo $name['FansubID'] ." ". $name['Designation'] ." ". $name['Website'] ." ". $name['tracker'] ." ". $name['irc'] ."<br>"; } The problem i have with the formating (i really dont know who i can do it) is when he reaches the IRC link he needs to open and close the td and tr brakets of the table. any help or tip is apreciated, thanks in advance Hey guys & gals, i have the following code: Code: [Select] <?php $date= $_SESSION['date']; echo $date; ?> Which is an echo of an earlier page: Code: [Select] <?php $_SESSION['date'] = date("c")?>; How do i format the echo of the first quote code? At present it echo's like so... Quote 2011-03-30T15:43:43+01:00 ...which although is useful for back office purposes, not so easy on the eye for the customer. How can i format so it's like DATE-MONTH-YEAR? I have googled this and there are examples, but none for when echoing a $_SESSION which is where i'm falling flat on my face. I tried this: but it doesn't work :-( Code: [Select] echo $startdate ("F j, Y") Thanks in advance to all who read and reply how to format only one specific row based on the value of one cell in a row? $result = mysql_query(select bal, etc. etc ..query ) while($row = mysql_fetch_assoc($result)) echo "<tr>"; echo "<td>" . $row['Account'] . "</td>"; echo "<td>" . $row['licence'] . "</td>"; echo "<td>" . $row['own'] . "</td>"; echo "<td>" . $row['bal'] . "</td>"; echo "</tr>"; Would appreciate help on how to format only the $row['bal'] output to red colour IF bal >5000 Thanks Anyone know how to cast arabic numeric input to (int) ? Or how to convert arabic numerals to english numerals? I created an auction website, inputting english numbers seems to work. However when I switch languages to Arabic, it doesn't work. By casting any arabic numeral input to (int) It keeps returning 0. ١٢٣٤٥٦٧٨٩ 123456789 Hi. I want to build a SQL string based on my form fields so when I have a new field in the db, I can just add a new form field and the sql update will automatically work. For example I have:
<input type=text name=t_something> <input type=text name=d_something> <input type=text name=n_something>(post submission) $data = array(); foreach ($_REQUEST AS $key=>$val) { if (substr($key, 0, 2) == "t_") $data[substr($key, 2, strlen($key)-2)] = $val; if (substr($key, 0, 2) == "n_") $data[substr($key, 2, strlen($key)-2)] = (trim($val)==""?"0":$val); if (substr($key, 0, 2) == "d_") $data[substr($key, 2, strlen($key)-2)] = (trim($val)==""?"NULL":date("Y-m-d", strtotime($val)); } $data["posted"] = "NOW()"; $sql = "INSERT INTO table ("; $cnt = 0; foreach ($data AS $key=>$val) { $cnt++; $sql .= $key . (($cnt < count($data))?", ":""); } Hi, My errorlog keep spamming the following errormsg: "PHP Notice: A non well formed numeric value encountered on line 365". This is the code it refers to: Code: [Select] for ($yr = $year, $age = -1; mktime(0, 0, 0, $month, $day, (int)$yr) < $today; $yr++, $age++); It's supposed to calculate the owner of the page's age. The full code is: Code: [Select] list($year,$month,$day) = explode("-", $birthday); $tempVar=list($year,$month,$day) = explode("-", $birthday); $today = time(); for ($yr = $year, $age = -1; mktime(0, 0, 0, $month, $day, (int)$yr) < $today; $yr++ Hi, After making an update, I have received this notice: Notice: A non well formed numeric value encountered in -... -on different page on line 128 : <?php $identifier = round(microtime() * 10000000); ?> on line 402 : <?php $identifier = round(microtime() * 10000000); ?> on line 2: <?php $identifier = round(microtime() * 10000000); ?>
How can I fix it? Thanks so much Federica Hey Guys...I am trying to secure my php file and have been reading a lot regarding sql injection. I still dont understand clearly how to prevent sql injection through numeric data input, since from what I understood mysql_real_escape_string() does nothing about it only prevents attacks on string input. Here's an example: if ($action == "checkId") { //retreive data from flash $user_id=mysql_real_escape_string($_POST['Id']); $result = mysql_query("SELECT user_id from users WHERE user_id = '$user_id'"); if (mysql_num_rows($result) > 0) { echo "status1=exists"; } else { echo "status1=id doesnt exist"; } } I would like to create a function like this: foreach($_POST as $post) { $postvars[$key] = htmlentities($post); //XSS prevention $postvars[$key] = mysql_real_escape_string($post); //Sql String Prevention } But then again...How do I check on the numeric POST's ? how do I validate them through this function? Any suggestions and/or ideas? Thanks a lot in advance! Cheers. |