PHP - Separate Names In Loop With Explode
trying to create an array that is separating brackets, semi-colon and space from "mydata" and iterating over to create first and last name's separated into two subs... and then loop those through. so i'd like either change the way i can explode and loop the information... or place the first and second subs and skip the third sub in the array.
$str = $_POST["mydata"]; $mores = explode("[];",$str); foreach ($mores as $more) { $datas = explode(" ",$more); if (array_values ($datas) === $datas) $xmlBody .= " <member name='$datas[1], $datas[0]' display='$datas[0] $datas[1]'>Name</member>"; } can someone please assist? much appreciated. Similar TutorialsHello, If I have this string: $tags="baseball glove face" (this can vary from 1 to 15 different words) how would i loop through and divide these into different $tag1=baseball $tag2=glove $tag3=face .. and so on (if more words) Thanks for help This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=321230.0 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? My Php Buddies, I have mysql tbl columns these:
id: Now, I want to display their row data by excluded a few columns. Want to exclude these columns: date_&_time account_activation_code account_activation_status id_verification_video_file_url password
So, the User's (eg. your's) homepage inside his account should display labels like these where labels match the column names but the underscores are removed and each words' first chars CAPITALISED:
Id: 1
For your convenience only PART 1 works. Need help on Part 2 My attempted code:
PART 1 <?php // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Query to get columns from table $query = $conn->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'members' AND TABLE_NAME = 'users'"); while($row = $query->fetch_assoc()){ $result[] = $row; } // Array of all column names $columnArr = array_column($result, 'COLUMN_NAME'); foreach ($columnArr as $value) { echo "<b>$value</b>: ";?><br><?php } ?> PART 2 <?php //Display User Account Details echo "<h3>User: <a href=\"user.php?user=$user\">$user</a> Details</h3>";?><br> <?php $excluded_columns = array("date_&_time","account_activation_code","account_activation_status","id_verification_video_file_url","password"); foreach ($excluded_columns as $value2) { echo "Excluded Column: <b>$value2</b><br>"; } foreach ($columnArr as $value) { if($value != "$value2") { $label = str_replace("_"," ","$value"); $label = ucwords("$label"); //echo "<b>$label</b>: "; echo "$_SESSION[$value]";?><br><?php echo "<b>$label</b>: "; echo "${$value}";?><br><?php } } ?> PROBLEM: Columns from the excluded list still get displayed. Edited November 19, 2018 by phpsaneHello 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, 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 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 ?> 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? Hi all! This is probably fairly simple for someone who is used to dealing with arrays. I have an array of data(Parsed from an XML document) containing a string I would like to further split into another array. The string is along the lines of: "<name>":<value>,"ID":20251,"ID2":2300,"ID3":2000 How can I split the above into: Array[<NAME>] => <VALUE> Notes: <name> changes often. depending on the query, it may have different <name> values. I have been trying to do it with preg_split and got to: Array ( => "<NAME>":<VALUE> [1] => "ID":20251) but I need to split it further at ":". I tried a foreach, but I failed miserably. Can anyone point me in the direction of better practice for arrays/preg_split? I have looked into the PHP documentation, but it is not enough for me. Thanks in advance 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 store date data in MySql in this format DD-MM-YYYY , and now I need to explode this format when get information from database to get this: $day = DD , $month = MM , $year = YYYY. $result = $db->query("SELECT user_birthday FROM users WHERE user_id = " . mysql_real_escape_string($_SESSION['user_id']) . " LIMIT 1"); $row = mysql_fetch_assoc($result); $birthday = $row['user_birthday']; and I get this date format: 12-07-1980 How to explode this 3 values in day, month, year? I 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! 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. how to take a word in php or explode based on luas Bangunan :xxx For example I have string $string =" Kondisi Properti : Bagus Dilengkapi Perabotan : Unfurnished Sertifikat : Lainnya Daya Listrik : 2200 Watt Kamar Tidur : 3/1 Kamar Mandi : 2/1 Luas Bangunan : 92 m² Luas Tanah : 126 m² Jumlah Lantai : - Kondisi Properti : Bagus Sekali Dilengkapi Perabotan : Unfurnished Sertifikat : SHM - Sertifikat Hak Milik Daya Listrik : 6600 Watt Saluran Telepon : 1 Garasi : 3 Kamar Tidur : 4/1 Kamar Mandi : 3/1 Luas Bangunan : 300 m² Luas Tanah : 228 m² Jumlah Lantai : 2.5 "; eg I want to take every "Luas bangunan: xxx" Thanks I have a script that connects to another server to pull data... When it pulls the data it comes back in the following format: Code: [Select] t1.sh t2.sh t3.sh test.sh However using the code bellow does not separate it like it should, it should put one file per line but I think I did something wrong... Code: [Select] // Pulls the output from the server: $var_test = $ssh->exec('./test.sh'); // Explodes it... $var_test2 = explode(" ", $var_test); // Echos it one per line... foreach ($var_test2 as $variable) { echo '<br>'.$variable; } Lets say I have a URL of http://mydomain.com/somename/ I need a function to grab USERNAME However I need it to work if someone uses http://www.mydomain.com/USERNAME/ OR With or without the trailing / after USERNAME I can't see to figure anything out which accounts for the many different variables that a person could type it. And yet another way is that the URL could be http://www.mydomain.com/USERNAME/SOME-CAT/SOME-POST/ Thanks Hi, I have a database that is going to contain recepies and ingredients tables. The recipe table is going to store the ingredients like this: 1,5,6,8 This refers to the table ingredients id. I now want to explode these vaules and count them up so they can be outputted like this: sugar (2) honey(1) flour (3) I have no idea of how to count all of the exploded id's and ouput them like above Ok so what I have is a list of 32,000 IDs in a separate file called 32kIDs.txt and it has a list of roughly 32,000 user IDs for facebook which look like 100000233850312;1521225383;100000282767638;1252335882;1804268242 They're all separated by a semi-colon. What I need this script to do is echo each and every ID on screen, echoing each ID on a new line. I have been using the explode function but having trouble with it since I'm very new to PHP and don't have much experience yet. Here is my current code: <?PHP $iFile = "32kIDs.txt"; $login = file($iFile, FILE_SKIP_EMPTY_LINES); if(!is_file($iFile)) echo "The 32k add list couldn't be found...".sleep(999999); foreach($login as $line_num => $line) { $login = explode(" ", htmlspecialchars(str_replace(";"," ",$line))); echo $login[0]."\n"; } sleep(20); ?> So I bet there are going to be things in there that I don't even need and thing's I'm missing, if anyone can help it would be much appreciated. And I'd like to keep the code short, I could just put in: echo $login[0]."\n"; echo $login[1]."\n"; echo $login[2]."\n"; echo $login[3]."\n"; all the way up to echo $login[32246]."\n"; but yeah I don't like the thought of a 32k+ line script. Anyways... |