PHP - Does Explode() Negative Limit Make Array_pop() Useless
I'm curious, is there any reason to use
array_pop($value) at all when you can explode(' ', $value, -1) ??? Besides being php version < version 5.1.0 compatible of course. Similar TutorialsI am in need of a alimit on the explode() fucntion but reading through the php explode manual on php.net there doesn't seem to be one that is right for me. Basically I'm making a command system for a php chat and it has a command called ban. I have code that find out if it is a command or a normal string, I also have code that starts to break up the command into segments that can be places in the database. I want to split up "ban user lengthID "Reason why you were banned"" using explode($string, " ") but I notice that that will also break up the "reason why you are banned". How do I get it to split up the command but NOT thereason why you were banned string? Confusing I know, Please ask any questions that would help me explain it better Thanks in advance! How do I reverse the logic of my Regex statement so it says, "If there is NOT a match..." Code: [Select] // (Replacement for non-supported Email-Filter.) if (preg_match('#^[A-Z0-9_\+-]+(\.[A-Z0-9_\+-]+)*@[A-Z0-9-]+(\.[A-Z0-9-]+)*\.([A-Z]{2,7})$#i', $trimmed['senderEmail'])){ // Valid Email. // ??? }else{ // Invalid entry. $errors['senderEmail'] = 'Please enter a valid E-mail address.'; }// End of VALIDATE ENTRY. I want to do this so I don't have an empty THEN statement! Debbie is it possible to calculate sum of the element in the array_pop . i have to get sum of 3 maximum subject with grade from student .
else if (($sub == 'BI') || ($sub == 'TSI') || ($sub == 'PAP') || ($sub == 'PAK')) { if ($res == 0) { $merit = 18; } if ($res == 1) { $merit = 16; } if ($res == 2) { $merit = 14; } if ($res == 3) { $merit = 12; } if ($res == 4) { $merit = 10; } if ($res == 5) { $merit = 8; } if ($res == 6) { $merit = 6; } if ($res == 7) { $merit = 4; } if ($res == 8) { $merit = 2; } if ($res == 9) { $merit = 0; } $main_add_merit1 = $merit; $main_add_merit1; $y[$i] = $main_add_merit1; //echo '<br>'; rsort($y); $first = array_pop($y); }
If I explode a string well using a substring when I implode that substring is now missing. for example. Code: [Select] $exploded = explode("[MAC]",$data); $data = implode($exploded); Now [MAC] would be missing in the $data string. any way to get around this?
I want to use array_pop to remove the last item added to the SESSION array. So it would remove the REQUEST item. The problem is that nothing is left in the session array because I pop off the last item each time that the form is submitted. Maybe I need to use array_push to push the popped items back on to the SESSION array after the logic has been applied. I don't understand how array_pop is supposed to work on a SESSION array. Can anyone explain to me what behaviour to expect? I expected to get a SESSION array with the REQUEST item removed but I thought that I would still have all of my SESSION items. Unless buffer overflows or breaking out of code to perform a new command are problems that have been solved.... I am trying to figure out the proper PHP method for setting a boundary on a variable within a script. I have this variable $name which is fed a value from $_POST['name'] from a form field. Now this form field is limited in the HTML to accept only 20 characters, but someone could easily edit the form or outgoing post data. So I want to know how to limit the variable size in the script. In other languages it could be something like this: var name(20). So how do I do that in PHP? Hi Guys
Bit stuck on a negative lookahead and not sure that what i want to achieve is possible.
Lets says I have a domain: www.mysite.com
I want to have a regex that will match www.mysite.com?q=123&g=34 but I don't want to match any file extensions like .php or .html
So the first part of matching the query string and the literal url is fairly straightforward (below) which matches any query string that may be applied
/^www.mysite.com[\/]?[\?\=\&a-z0-9]*$/But then I want to use a negative lookahead to avoid matching a file extension, e.g. /^www.mysite.com[\/]?[\?\=\&a-z0-9](?!php|html|htm)$/But this doesn't work - presumably because the negative lookahead is following a character class. So my questions: can you use a negative lookahead in this way? If not, how can I achieve what I'm trying to? Any help appreciated! Drongo <?php if ($beyondportal % 26 == 0 || $beyondportal == 0){ ?> MY CONTENT <?php } ; ?> This code compares my variable $beyondportal and if it is a multiple of 26 counting up from 0, it prints my content. This is great but sometimes the number comes up a negative that is a multiple counting down from 0 like -26, -52, etc.. if I could do the opposite of a modulus I would think it would work but I'm not sure how to in php. any suggestions? Hi all I am adding some positive and negative numbers. But i am getting strange output. Can anyone help me Here is the code. Code: [Select] $sum=7.50+6.45+12.90+12.00+13.00+0.61+32.00+4.00+27.00+18.00+10.88+129.50+92.94-100.00-38.25+76.80-305.33; echo "<br>Total: ".$sum; Basically i have to get Total is 0 but i am getting total is 5.6843418860808E-14 Can anyone help me out Please I have a code to sum up a field called quantityHand in MySQL, in my example below, the value always return 40 as a positive number instead of 40.00- 0.00 0.00 40.00- I have used intval() or floatval() function to convert the type of variable from string to int/float but still not working. Any help please? Here is the code Code: [Select] $sql = mysql_query("SELECT DISTINCT itemNumber, itemDesc, quantityHand, SUM(quantityHand) AS quantityHand FROM inventory where itemNumber like '%$term%' GROUP BY `itemNumber` ORDER BY `itemNumber`"); while ($row = mysql_fetch_array($sql)){ echo "<b>"; echo "</td><td style=\"text-align: center;\"><b>"; echo $row['itemNumber']; echo "</td><td style=\"text-align: center;\"><b>"; echo $row['itemDesc']; //echo "</td><td style=\"text-align: right;\">"; echo "</td><td style=\"text-align: center;\"><b>"; echo $row ['quantityHand']; echo "</td></tr>"; echo "</b>"; Hi, How can I check if a variable only has these characters: - number (0-9) - decimal point (.) - positive (+) - negative (-) - dollar ($) I tried is_numeric, but it doesn't like negative values... I need to add the variable value to another variable. I have to accept numbers (including negative and decimals)... ex: 0.1 = TRUE -.1 = TRUE 12 = TRUE 1000.00 = TRUE 12a = FALSE thanks This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=353188.0 Hello Ive got a text area and i want to seperate each link that is placed inside the text area,. Is there a way using php exlode or somthing to reconise that the link has finished? Each of the links in the text area are on a new line. Thanks in advance. Hi all, I posted a similar question before and got some of the way but still need a little help. I have a product database with a description column where there are strings broken into lines. I need to find a way to explode() by line break. Currently explode("/n") doesn't work - it only returns one array item. Can anyone help me out? Thanks! i want to explode a text field on the fly ? for example city,region.country and to turn it to city_id,region_id,country_id 1.textfield from autocomplete 2.get post variable 3.explode 4 get ids i played around with some ajax with php Code: [Select] <html> <body> <input name="searchField" id="searchField" type="text" value="<?php echo $_POST['searchField'];?>"/> <script type="text/javascript"> function Ajax(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer }catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ alert("No AJAX!?"); return false; } } } xmlHttp.onreadystatechange=function(){ document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText; var t=setTimeout('Ajax()',1000); clearTimeout(t); } var searchfieldvalue=encodeURIComponent(document.getElementById("searchField").value) xmlHttp.open("GET","getid.php?searchField="+searchFieldvalue+"",true); xmlHttp.send(null); } window.onload=function(){ var t= setTimeout('Ajax()',1000); } </script> <?php /*explode searchField and generate ids $searchField=$_POST['searchField']; $p = explode(" , ", $searchField, 3); $cityname=mysql_real_escape_string($p['0']); $regionname=mysql_real_escape_string($p['1']); $countryname=mysql_real_escape_string($p['2']); */ ?> <div id="ReloadThis">Default text</div> </body> </html> //getid <?php $p=array(); $p = explode(" , ", $_GET['searchField'], 3); echo $cityname=$p['0']; echo $regionname=$p['1']; echo $countryname=$p['2']; echo'<input type="hidden" name="place" id="place" value="'.$cityname.'"/> <input type="hidden" name="region" id="region" value="'.$regionname.'"/> <input type="hidden" name="country" id="country" value="'.$countryname.'"/>'; here ill add function to get city_id() etc ?> Hi, I have an array for prices : forfait="158|Group Session,95|Group half session,45|Private Course,90|zumba course" which is used in a dropdown list where it shows as Group Session (158$) Group half session (95$) etc... The prices need to be used as an 'amount' field on the gift certificate as the descriptions must show as a 'label' on the gift certificate. So, I need to ' explode ' both the prices and the descriptions separately. I can 'explode' de prices using $tmp_forfait=explode("|", $value) and I get 158 95 45 and 90 But, how do I get the descriptions separately, without the prices meaning I need to 'explode' the digits and '|' ... Anyone can help me with that cope please? Thank you With a database of synonyms, how do I compare a fragment of a texarea sentence split by the php explode function to return only five synonyms out of say 100 synonyms of the same word for each fragment. The Sql statement is queried to return 5 results Code: [Select] <?php if (isset($_GET['submit'])) { $sentence = $_GET['sentence']; // break $sentence using the space character as the delimiter $words = explode(' ', $sentence); //tell the amount of words using the size of the array echo 'The sentence has ' . count($words) . ' words.<br />'; // loop through and print all the words for ($i = 0; $i < count($words); $i++) { echo "Piece $i = $words[$i] <br />"; //echo 'Word ' . $i . ' - ' . $words[$i] . '<br />'; //$words=$_GET['words']; $sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3"; $data=mysqli_query($dbc,$sel); $num= mysqli_num_rows($data); if (!$data) { die('<p>Error Retrieving<br/>'. 'Error: ' .mysql_error() . '</p>');} $datalist=mysqli_fetch_assoc($data); $synonym=$datalist['synonym']; ?> I intend to use a 2 texareas: 1. Code: [Select] <textarea name="split_sentence"><?php echo "sentence" ?></textarea> the 1st <texarea> has the original sentence to be split by the explode function. 2. Code: [Select] <textarea name="split_sentence2"><?php echo ("$words[$i] {{$synonym}}?></textarea> the second <texarea> should take the explode fragments compare it with the query above and return five synonyms per word on a SINGLE <textarea> tag. 3. Besides, I keep getting this offset error on the 2nd <texarea> refering to the $words[Si] array element in the 2nd <textarea> Below is the full code: Code: [Select] `<?php //session_start(); // Define database connection constants define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_NAME', 'moby_thesaurus'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>compare_words</title> </head> <body> <table><p> PLEASE ENTER INITIAL SENTENCE </p> <form name="sentences" method="get" action="" > <tr> <td> <textarea name="sentence" id="elm1"></textarea> </td> </tr> <tr> <td> <input name="submit" type="submit" value="Create_Spinable Content" /> </td> </tr> </form> </table> <?php if (isset($_GET['submit'])) { $sentence = $_GET['sentence']; // break $sentence using the space character as the delimiter $words = explode(' ', $sentence); //tell the amount of words using the size of the array echo 'The sentence has ' . count($words) . ' words.<br />'; // loop through and print all the words for ($i = 0; $i < count($words); $i++) { echo "Piece $i = $words[$i] <br />"; //echo 'Word ' . $i . ' - ' . $words[$i] . '<br />'; //$words=$_GET['words']; $sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3"; $data=mysqli_query($dbc,$sel); $num= mysqli_num_rows($data); if (!$data) { die('<p>Error Retrieving<br/>'. 'Error: ' .mysql_error() . '</p>');} $datalist=mysqli_fetch_assoc($data); $synonym=$datalist['synonym']; ?> <table><p> SPINABLE CONTENT </p> <form name="sentences" method="get" action="" > <tr> <td> <?php if ($datalist=mysqli_fetch_array($data)) { do{ ?> <textarea cols="80" id="editor1" name="sentence" rows="10"><?php echo ("$words[$i] {{$synonym}}||{{$synonym}}||{{$synonym}}"); ?></textarea> <?php } while ($datalist=mysqli_fetch_array($data)); echo("</table> </p>"); } ?> <?php } } ?> </td> </tr> <tr> <td> <input name="submit" type="submit" value="Spin This Now" /> </td> </tr> </form> </table> </body> </html> Please any suggestions would be highly appreciated. I'm trying to add options to products. Currently, I have them in this format: Leather reinforced hilt,5.00::Stabbing tip,3.00::Logo,10.00 Code: [Select] $option_results = mysql_query("SELECT product_options FROM products WHERE product_id='".$product."'"); while($option_row = mysql_fetch_array($option_results)){ $option = explode("::", $option_row[0]); $option_part = explode(",", $option[0]); $option_part[0] = $option_name; $option_part[1] = $option_price; echo "<li><a href=\"#\">".$option_name.", Add $".$option_price."</a></li>"; } I would like the output to be: <li><a href="#">Leather reinforced hilt, Add $5.00</a></li> <li><a href="#">Stabbing tip, Add $3.00</a></li> <li><a href="#">Logo, Add $10.00</a></li> Code: [Select] $newrating = round($imageinfo['rating'],2); this outputs: 4.49 in my db rating is 4.4884 all i need is the 4.... would i use some type of preg_replace or something just to grab the first 1 before the decimal? (all i need is the first number before the decimal) I tried to do this: explode(" ",$cp['size'])[0] and it isn't working obviously. The reason I want it in one line is its in the middle of a big string and I'd like to keep all the code in one area. Is their a right way to do that other than setting it to a variable first? |