PHP - Parse An String By A Character
I have a $string with this structure "word-word2 something-SECONDPART". I want to divide it to $string1 and $string2 by separating with the last "-".
The point is that "-" exists in the first part, and I just want to make the second string after the last "-" Similar TutorialsHow do you find out how many character within a String ?? for example: $temp = "apple"; I want to return "5" for $temp as it has 5 characters. Thanks! i have to tables... forums & subscription i would like to insert a random character string into 'forums' to be used as a subscription id. and then have the same random string to be inserted into 'subscription' How can I check this string to see if it has a ~ character using php? Code: [Select] $string = 'userlocation~credentials~specialties'; I am trying to find a character in the follwing string $string = ' Mumbai Mast Kallander - 02 - Mumbai Mast Kallander'; the character i want to find is '-' and the find how many times does it occur in the string.. can anyone help? Hey all, I'm looking to produce a random 10 character string like: abcABC0123 when the below method is called: <?php function randomize(){ $chars = explode(' ',(range("a","z"))) . explode(' ',(range("A","Z"))) . explode(' ',(range("0","9"))); $pass = ""; for($i = 0; $i < $len; $i += 1){ array_push($pass,$chars[rand(count($chars)-1)]); } return $pass; } var_dump(randomize(10)); ?> But the randomize method is not returning desired result. Any idea what I'm doing wrong? Thanks for response. How to remove the last character of a string if it is "-" ? In replacing some elements in strings, sometimes "-" remains at the end. How to trim the string by removing "-", if it is at the end of the string? I have this command that basically populates my website with titles for pages based on the information that is placed within "info.txt" I have multiple titles being created so I'd like a way of putting them in order beside alphabetically. My thought was that I could place numbers as the first character in the "info.txt" file and that would allow me to put the titles in an order I find acceptable. Now this causes the issue that I don't want those numbers displayed on my website. So the Echo needs to strip the number.... I'm lost, but my brain thinks it's a good idea. Here's the code I'm working with: Code: [Select] <tr> <td id="body"> <table id="columns" cellspacing="0"> <tr> <td> <h2>LSAV Latest Events</h2> <p>Select an event from below to view a gallery of images from that event.</p> <ul> <?php $dir = 'latest-events'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if( $file != '.' && $file != '..') { $title = file_get_contents( "$dir/$file/info.txt" ); echo "<li><a href=\"event-viewer.php?event=$file\">$title</a></li>"; } } closedir($handle); I was wondering if anyone knows a good way to check for an even number of each character in a string. For example if the string is "abbacc" then it will return true as there are two of each character. However if the string is "ababc" it will return false as there is only 1 c. Thanks for any help I have a form with some dropdownlist that get populated dynamically(depends on what the user chooses) with jquery and an xml file. In the form I have a button that takes the info and populates another form with the values of the ddl. I have coded script which populates the second form with jquery and the name of each input get dynamically added like so: Code: [Select] <input type="text" value="'+model+'" name="Model_'+model+'" id="'+model+'" readonly size="'+model.length+'" /> <input type="text" value="'+model+'" name="Model'+model+'" id="'+model+'" readonly size="'+model.length+'" />> <input type="text" value="'+color+'" name="Color'+color+'" id="'+color+'" readonly size="'+color.length+'" /> <input type="text" value="'+part+'" name="Part'+part+'" id="'+part+'" readonly size="'+part.length+'" /> <input type="text" value="$'+price+'" name="Price'+price+'" id="'+price+'" readonly size="'+price.length+'" /> <input type="text" value="'+qty+'" name="Quantity'+qty+'" id="'+qty+'" readonly size="'+qty.length+'" /> <input type="text" value="'+total+'" name="Total'+total+'" id="'+total+'" readonly size="'+total.length+'" /> What I want to do is grab the name of the element and remove the underscore and everything else form rileft to right so I am left with just the word before the underscore. I tried using substr($key,0,-28) but that wont work since everything after the underscore will have a different length. I was wondering if there is a way i can keep everything before the underscore and remove the underscore and everything after that. Thanks in advance for any input I get. I have a kind of search and react on my site. The user can search for a number in a column as below $search = mysqli_real_escape_string($link, $_POST["query"]); $query = "SELECT * FROM $table[$i] WHERE oemnr LIKE '".$search."%' ORDER BY model";
The code works fine but I want to expand the function to get match for other numbers also. The oemnr column today can be 12345 which shows up if the user type 12.... I want to expand the oemnr column as 12345/03124/34713, numbers separated with / or another character. If the user type 03... or 34... I want to find and show 12345 because it is on the same row and corresponds to the numbers on the same row. Is it possible to do in mysql? In php that kind of operation can be done with the explode function but I don't know how to use with mysql. Edited February 14 by jiggaHello to everyone Can someone help me on how to parse this CSV string? I do not know how to parse string using PHP.. #1# "4","+447980123456","+447781484145","","2009-07-08","10:38:15","hello "","" world","Orange" "5","+447980123456","+447781484146","","2009-07-08","10:38:55","hello world","Orange" This string can contain a much bigger data. I would like the result to be Originator : +447781484145, Date : 2009-07-07, Time : 10:38:15, Message : hello world Originator : +447781484146, Date : 2009-07-08, Time : 10:38:55, Message : hello world Please guys if anyone of you knows how to parse this string it will be much appreciated. Im nearing my deadline for my work and this thing is keeping me from finishing it.. Thanks in advance I need parse the JSON string below into an associative array, sort it by the 'age' field and output the sorted array as an HTML table. Here is what I have done so far but it just returns me 'Array': Code: [Select] <?php $json='[{"name": "John","age": 23},{"name": "jim","age": 19},{"name": "jason","age": 34}]'; $array = json_decode( $json, true); function cmp( $a, $b){ if( !isset( $a['age']) && !isset( $b['age'])){ return 0; } if( !isset( $a['age'])){ return -1; } if( !isset( $b['age'])){ return 1; } if( $a['age'] == $b['age']){ return 0; } return (($a['age'] > $b['age']) ? 1 : -1); } usort( $array, 'cmp'); foreach ($array as $key => $value) { echo $key.":".$value."\n"; } ?> I'm trying the fetch some geocode data from mapquest. Google doesn't like me right now. I keep running into the query_limit. So I want to set a back up that will go into production when I get that error from the big G. I have my api key and I can put the string in a var. Code: [Select] $geocode = file_get_contents($url) I selected json as the output and when I echo $geocode... here is the first portion of the string....(I want lat & lng) Code: [Select] renderOptions({"results":[{"locations":[{"latLng":{"lng":-112.35984,"lat":34.58752},..... Now for the life of me, I can't do anything with it. I've tried json_decode() with the true argument there and not. Code: [Select] $output = json_decode($geocode, true); $output = json_decode($geocode); vardump($output) is NULL. So, I thought, well maybe I don't need to decode it, but I can't figure out how to parse it. I'm lost. This admittedly is my first attempt as trying to work with a jason object, but jeesh, it doesn't seem like it should be that hard. Can someone start me in the right direction? I have a list of pictures and I want to control the order. Here is the picture format: $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_1.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_2.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_3.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_4.jpg $pic = http://www.picture.biz/incoming/w_2GTEK13T961240561_5.jpg Notice the order 1.jpg, 2.jpg, etc.. Here is where I'm stuck... // begin stuck :) $orderPic = explode("_", $pic); foreach($orderPic as $rank){ echo "$rank"; // I need $rank to equal 1 then 2 then 3 ... } It would be nice actually if I could start the insert with number 2 because number one is always a screwy picture. I hope this makes sense / Thank for the help! Say you have a string that you know has two, and exactly two, periods in it, like this...
$string = "3.12.8"How can you grab the three different values that are separated by periods (in this case, 3, 12 and ? I was thinking use strpos to find the position of the first period, then could use that info to grab everything up to that position. But then I couldn't figure out how you would find the position of the SECOND period. And even if I could get the exact positions of the two periods, it seems like there should be an easier way to simply retrieve values separated by specific characters. Is there a specific function for this type of thing, or was my initial though correct with having to do it in two parts essentially (find positions of periods, then use that knowledge to get the values)? Thanks! Hello I have one problem with fwrite() I have one script to get width and height from javascript and echo it with PHP. echo "Screen width is: ". $_GET['width'] ."<br />\n"; echo "Screen height is: ". $_GET['height'] ."<br />\n"; It works but i want to store the result in a file fwrite($info,"Height: $_GET['height'] <br />"); But then I get error Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING //////////////////////// it would be even better if I don't have to print the values but directly store them in $iinfo sorry if something similar was soved but those examples were different or i was unable to transform it to solve my problem hi all am new to this forum help me to overcome from this error Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in D:\wamp\www\quiz1\quiz1.php on line 12 Code: [Select] <?php include("contentdb.php"); $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); if (!$submit) { echo "<form method=post action=$_SERVER['PHP_SELF']>"; echo "<table border=0>"; while ($row = mysql_fetch_array($display)) { $id = $row["id"]; $question = $row["question"]; $opt1 = $row["opt1"]; $opt2 = $row["opt2"]; $opt3 = $row["opt3"]; $answer = $row["answer"]; echo "<tr><td colspan=3><br><b>$question</b></td></tr>"; echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>"; } echo "</table>"; echo "<input type='submit' value='See how you did' name='submit'>"; echo "</form>"; } elseif ($submit) { $score = 0; $total = mysql_num_rows($display); while ($result = mysql_fetch_array($display)) { $answer = $result["answer"]; $q = $result["q"]; if ($$q == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; if ($score == $total) { echo "Congratulations! You got every question right!"; } elseif ($score/$total < 0.34) { echo "Oh dear. Not the best score, but don't worry, it's only a quiz."; } elseif ($score/$total > 0.67) { echo "Well done! You certainly know your stuff."; } else { echo "Not bad - but there were a few that caught you out!"; } echo "</p>"; echo "<p>Here are the answers:"; echo "<table border=0>"; $display = mysql_query("SELECT * FROM $table ORDER BY id",$db); while ($row = mysql_fetch_array($display)) { $question = $row["question"]; $answer = $row["answer"]; $q = $row["q"]; echo "<tr><td><br>$question</td></tr>"; if ($$q == $answer) { echo "<tr><td>»you answered ${$q}, which is correct</td></tr>"; } elseif ($$q == "") { echo "<tr><td>»you didn't select an answer. The answer is $answer</td></tr>"; } else { echo "<tr><td>»you answered ${$q}. The answer is $answer</td></tr>"; } } echo "</table></p>"; } ?> thanks in adavance I am trying to put a href on a line to link me to another php module, but get an error: Parse error: parse error in C:\wamp\www\editpolicy.php on line 47 My code: while ($row2 = mysql_fetch_array($result2) ) { print "<tr><td>"; <a href='editpayment.php?payid=$row2[PaymentID]'>$row2[PaymentID]</a> .........THIS IS LINE 47 ??? print "</td><td>"; print $row2['Actioned']; print "</td><td align='right'>"; print $row2['PremiumPaidAmount']; print "</td><td align='right'>"; print $row2['Risk']; print "</td></tr>"; } echo "</select></td>\r\n </tr>\r\n \r\n <tr>\r\n <td align=\"right\">Total Views Allowed:</td>\r\n <td><input type=\"text\" id=\"ad_total_views\" style=\"width:50px;\" value=\"\" disabled=\"disabled\" /> <label><input type=\"checkbox\" checked=\"checked\" onclick=\"if(this.checked==true){$('#ad_total_views').val('');$('#ad_total_views').attr('disabled','disabled');}else{$('#ad_total_views').val('');$('#ad_total_views').removeAttr('disabled');$('#ad_total_views').focus()}\" />Unlimited</label></td>\r\n </tr>\r\n \r\n <tr>\r\n <td align=\"right\">Total Clicks Allowed:</td>\r\n <td><input type=\"text\" id=\"ad_total_clicks\" style=\"width:50px;\" value=\"\" disabled=\"disabled\" /> <label><input type=\"checkbox\" checked=\"checked\" onclick=\"if(this.checked==true){$('#ad_total_clicks').val('');$('#ad_total_clicks').attr('disabled','disabled');}else{$('#ad_total_clicks').val('');$('#ad_total_clicks').removeAttr('disabled');$('#ad_total_clicks').focus()}\" />Unlimited</label></td>\r\n </tr>\r\n </table></td>\r\n </tr>\r\n <tr bgcolor=\"#FFFFFF\">\r\n <td class=\"td_th\" align=\"center\">Smarty Code (Developer)</td>\r\n <td>{\$ads->getAdCode(<span id=\"smartycode\">1</span>)}</td>\r\n </tr>\r\n <tr bgcolor=\"#FFFFFF\">\r\n <td class=\"td_th\" align=\"center\"> </td>\r\n <td><input type=\"button\" value=\"Create New Campaign\" onclick=\"new_ad()\" /></td>\r\n </tr>\r\n </TBODY></TABLE>\r\n<br />\r\n\r\n<TABLE cellSpacing=1 cellPadding=4 width=\"100%\" border=0>\r\n <TBODY>\r\n <TR class=\"td_title\">\r\n <TD colSpan=7>Ad Campaigns</TD></TR>\r\n <TR bgColor=#ffffff>\r\n \r\n <TD width=\"10%\" align=\"center\" class=\"td_th\"> </TD>\r\n <TD width=\"4%\" align=\"center\" class=\"td_th\">ID</TD>\r\n <TD width=\"29%\" align=\"center\" class=\"td_th\">Campaign Name</TD>\r\n <TD width=\"12%\" align=\"center\" class=\"td_th\">Start Date</TD>\r\n <TD width=\"11%\" align=\"center\" class=\"td_th\">End Date</TD>\r\n <TD width=\"17%\" align=\"center\" class=\"td_th\">Viewed / Views Allowed</TD>\r\n <TD width=\"17%\" align=\"center\" class=\"td_th\">Clicked / Clicks Allowed</TD>\r\n </TR>\r\n "; $BoxSize = array("smallbox" = array("length" => 12, "width" => 10, "depth" => 2.5), "mediumbox" = array("length" => 30, "width" => 20, "depth" => 4), "largebox" = array("length" => 60, "width" => 40, "depth" => 11.5)); |