PHP - Selective Adding Of A String To An Array
I have a file containing lines (I mean elements separated by <br>). How I can put these lines into an array, but only those which has a given phrase.
Example file Code: [Select] This is the first line<br> Second line is here<br> something else<br> something else<br> something more<br> I want to catch only lines which contain the word "something" and make an array. Similar TutorialsOk I was wondering is there a way I can select a mysql database, and order it but only show data of a certain criteria: for example Mysql database: ID | User | XP | Won | Lost 1 Freid 100 1 4 2 Smic 300 5 4 3 Retna 300 3 2 4 OBW 200 3 2 And then some php code that will retrieve this information but only display the users that have lost = 4 in a list. In other words some php code that will only list the players Freid and Smic from the msql database becuase they have Lost = 4 the code would not show Retna or OBW becuase there Lost is not = to 4 Any help would be great thanks Freid001 I have seen somewhere that you can add to a string like so $var = "text"; or $string ="var"; &$var = "text"; it is the latter that I am wanting to know more about. Using curl_multi, I have loaded up 3 url's and then printed the array. 1) How can I also set a timestamp on output to let me know when each url was run? 2) How can I explode the array to only display the data and timestamp, excluding the text "Array ( =>", "[1] =>", "[2] =>" and " )"? Code <?php function multiRequest($data, $options = array()) { // array of curl handles $curly = array(); // data to be returned $result = array(); // multi handle $mh = curl_multi_init(); // loop through $data and create curl handles // then add them to the multi-handle foreach ($data as $id => $d) { $curly[$id] = curl_init(); $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; curl_setopt($curly[$id], CURLOPT_URL, $url); curl_setopt($curly[$id], CURLOPT_HEADER, 0); curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); // post? if (is_array($d)) { if (!empty($d['post'])) { curl_setopt($curly[$id], CURLOPT_POST, 1); curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); } } // extra options? if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } // execute the handles $running = null; do { curl_multi_exec($mh, $running); } while($running > 0); // get content and remove handles foreach($curly as $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } // all done curl_multi_close($mh); return $result; } $data = array(array(),array()); $data[0]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json'; $data[1]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Black+Eyed+Peas&output=json'; $data[2]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Nirvana&output=json'; $r = multiRequest($data); print_r($r); ?> Output Array ( => Pearl Jam [1] => Black Eyed Peas [2] => Nirvana ) Preferred Output 01:00:01 Pearl Jam 01:00:02 Black Eyed Peas 01:00:03 Nirvana Hi, I currently have a string with some values in it: $a_random_string="some random text, goes here - $123<br>some more random text goes here - $53 (text 123 )<br>ANother line of text - $126"; Now I want to add all the values that have a '$' before the value eg; $123. So in this case I would end up with: 302 How would I do this? Thanks, mme Is it possible to add a query string for example some_var=jk84 to any sort of link be it, http://www.website.com , http://www.website.com/?some_id=4, or http://www.website.com/post=45&category=9 or http://www.website.com/somepost/ ? While adding that extra query string how can I make sure I'm not affecting the website's content or causing some script error? So I'm trying to create an Array from a comma separated string containing array{s}. How can I take a string full of comma separated hex values such as : <?php $myStr = ( "00ffff", "00cccc", "009999", "007777", "004444", "001111", "01ffff", "01cccc", "019999" ); ?> & turn it into 3 separated arrays like : <?php $myNewArrs = [ Array1 ( 3 ), Array2 ( 3 ), Array3 ( 3 ), ]; ?> which REALLY looks like : <?php $myNewArrs = [ Array ( "00ffff", "00cccc", "009999" ), Array ( "007777", "004444", "001111" ), Array ( "01ffff", "01cccc", "019999" ), ]; ?>
? Thank you & have a great afternoon!
Edited May 27 by AquariaXI I have been becoming more and more familiar with PHP over the past few months, but this problem stumps me. How can I take a string of words separated by spaces taken from an XML feed (example: word1 word2 word3 word4) and turn it into something like this: <a href="word1">word1</a> <a href="word2">word2</a> ... and so on. This has to be done on the fly, since it is taking the words from the XML feed, they change depending on the page. I want them to be in a vertical column, and be able to link each one to the search page for that word, so each link has to be different. An even more in-depth problem, how can I sort it then so that there are multiple columns if necessary, say one page only has 3 words, but another has 50. How can I split them up into multiple vertical columns, say with 10 words in each? Have I confused anyone yet? I have a php line like: echo "<p><b>Tags: </b>" . $row['tags'] . "</p>";
it looks like this on web page: how do i add hyperlink to each keyword like: Tags: <a href='category/keyword1'>keyword1</a>, <a href='category/keyword2'>keyword2</a>, <a href='category/keyword3'>keyword3</a>,...
So I'm querying my database to add the results (mapID's) into a PHP array. The MySQL query I used in the below code would usually return 10 values (only 10 mapID's in the database) Code: [Select] while($data = mysql_fetch_array(mysql_query("SELECT mapID FROM maps"))){ $sqlsearchdata[] = $data['mapID']; } Instead the page takes ages to load then gives this error: Quote Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 16 bytes) It says the error begins on the first line of the above code. I'm assuming this is not the right way to add the value from the MySQL array into a normal PHP array. Can anyone help me? Hi I have an array of items that I am currently displaying a foreach loop What I'm looking to do, is add a string, to all of items, a part from the last 4: foreach ($pager->getResults() as $items => $item) { echo $item->getName(); //need to add a string to this for the all but the last 4 } Thanks Hello everyone, I have this very short script I wrote with help from a book using arrays and the rand function. Basically it changes the images randomly on browser load or refresh. However, I am trying to add an additional, not sure what to call it, a string or another caption for each images. Instead of this being a caption, it will be a link, so a link will appear under each caption that goes to another page when it is clicked on. I tried adding another caption and adding a like to it but getting syntax error. Thanks everyone! Here is the code: Code: [Select] <?php $images = array( array('file' => 'image1', 'caption' => 'Caption 1'), array('file' => 'image2', 'caption' => 'Caption 2'), ); $i = rand(0, count($images)-1); $selectedImage = "graphics/{$images[$i]['file']}.png"; $caption = $images[$i]['caption']; ?> And here is where the images are written to the page: Code: [Select] <div id="stage"> <img src="<?php echo $selectedImage; ?>" alt="Random image" /> <p id="caption"><?php echo $caption; ?></p> </div> IC Here's the code: Code: [Select] $year_values = array('Any', 'N/A', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012'); $db_year = explode(', ', $row['YEAR']); foreach($year_values as $year_value) { $selected_year = null; $year_label = $year_value; if(in_array($year_value, $db_year)) { $selected_year = ' checked="checked"'; $year_label = "<b>$year_value</b>"; } echo '<input type="checkbox" name="year" value="' . $year_value . '"' . $selected_year . ' />' . $year_label . ' '; } ?> [/code That pulls the data of the database and puts it in check boxes. The user can check/uncheck certain boxes but the problem is when it goes to the verify page it has only one value from the array that was selected. On the verify page it just checks if a box was checked and then implodes the array before submitting it into the database. [code] $year_s=implode($year,", "); So like I said there is only one value from the array when there should be as many as the user checks. So basically I believe the value isn't added to the array. Hi I currently pass a URL to our website as follows: http://website.com/?nid=10 but want to extend this to include a keyword from google as well. For example http://website.com/?nid=10&kw={keyword}. However it may not always have a keyword so if it does not it needs to just use the nid array. This is the current script, how do I go about changing it so that if it just contains a nid and no keyword it will do one search of the array and return the result, and if it does have a nid&keywordod it returns something different. For example if it had nid=1 it would return 0844 000 0300 and if it had nid=1&kw=help it would return 0844 000 0350 Thats what I am looking to achieve. Is this possible and how do I change the array. I have had a little attempt but not sure what I am doing and just got coding errors. <?php $GLOBALS['ct_get_parameter_name'] = 'nid'; $GLOBALS['ct_default_number_id'] = 0; // source_id => phone number $GLOBALS['numbers'] = array( 0 => '0844 000 3000', 1 => '0844 000 0300', 2 => '0844 000 0301', 3 => '0844 000 0302', ); session_start(); if (array_key_exists($GLOBALS['ct_get_parameter_name'], $_REQUEST)) { $_SESSION['ct_source'] = $_REQUEST[$GLOBALS['ct_get_parameter_name']]; } function get_phone_number() { $numbers =& $GLOBALS['numbers']; $default_number_id = $GLOBALS['ct_default_number_id']; if (isset($_SESSION['ct_source'])) { if (array_key_exists($_SESSION['ct_source'], $numbers)) { return $numbers[$_SESSION['ct_source']]; } else { // otherwise return first number return $numbers[$default_number_id]; } } else { // return first number return $numbers[$default_number_id]; } } ?> Many thanks in advance. Roy from what i understand from the coding... $form->getField('uri') is for the Sql table field (field uri) location and ->getValue() is the inputbox Value so ->setUri is grabbing the inputbox value and inserting it into Field 'uri' but what I need to do is combine getValue with the string/url profile.site.com/file?name= Code: [Select] $new_uri ->setUri($form->getField('uri')->getValue()) ->setHost($form->getField('host')->getValue()) ->setUser($user->isAuthorized() ? $user->getId() : 0) ->save(); I've got a site running, and I would like to add up the totals from each location to one number, now I know it can be done, I just don't know how it'll work with my current code (see below) <table summary="" border="1" width="1100"> <tr> <td>Date</td> <!-- <td>Employee</td> --> <td>Store</td> <td>Customers</td> <td>Quotes</td> <td>Phone Calls</td> <td>Services In</td> <td>Services Out</td> <td>Invoices</td> <td>Conversion</td> <td>End of Day</td> <td>Emails</td> <td colspan=3>Notes</td> </tr> <? $x = 1; $y=1; $lastdate=0; while ($list = mysql_fetch_assoc($query)) { $dateadd = $list['dateadd']; $dateb = strtotime($list['dateadd']); $date = date("M j Y", $dateb);$date2 = date("Y-m-d", $dateb); $firstname = $list['firstname'];$lastname = $list['lastname'];$store = $list['store'];$customers = $list['cust'];$sales = $list['sales'];$invoices = $list['invoices'];$sin = $list['servin'];$sout = $list['servout'];$eod = $list['eod']; $phone = $list['phone']; $notes = $list['notes']; $emails = $list['emails']; if ($store == "1") {$storename = "Broadmead";} elseif ($store == "2") {$storename = "Duncan";} elseif ($store == "3") {$storename = "Surrey";} elseif ($store == "4") {$storename = "Sooke";} elseif ($store == "5") {$storename = "Nanaimo";} elseif ($store == "6") {$storename = "Shelbourne";} ?> <?php if ($lastdate == $date) { ?> <tr> <td width="125"><?=$date;?></td> <!-- <td width="125"><?=$firstname;?> <?=$lastname;?></td> --> <td width="100"><?=$storename;?></td> <td width="71"><?=$customers;?></td> <td width="71"><?=$sales;?></td> <td width="80"><?=$phone;?></td> <td width="80"><?=$sin;?></td> <td width="90"><?=$sout;?></td> <td width="71"><?=$invoices;?></td> <td width="71"><? $convbase = $sout+$sales; if (!$invoices==0 && !$customers==0) {$conv = $invoices / $customers * 100;} else {$conv=0;} echo money_format('%(#1n', $conv) . "\n"; ?>%</td> <td width="71">$<?echo money_format('%(#1n', $eod) . ""; ?><td> <td width="71"><?=$emails;?> </td> <td width="150"><?if (!$notes==NULL){echo $notes;} else {echo " ";}?></td> </tr> <?php } else { ?> <tr> <td colspan="9">Daily Total</td> <td><?echo money_format('%(#1n', $total) . ""; ?> </td> <td colspan="4"> </td> </tr> </table> <hr width="1100" align="left" color="0000ff"> <table summary="" border="1" width="1100"> <tr> <td width="125"><?=$date;?></td> <!-- <td width="125"><?=$firstname;?> <?=$lastname;?></td> --> <td width="100"><?=$storename;?></td> <td width="71"><?=$customers;?></td> <td width="71"><?=$sales;?></td> <td width="80"><?=$phone;?></td> <td width="80"><?=$sin;?></td> <td width="90"><?=$sout;?></td> <td width="71"><?=$invoices;?></td> <td width="71"><? $convbase = $sout+$sales; if (!$invoices==0 && !$customers==0) {$conv = $invoices / $customers * 100;} else {$conv=0;} echo money_format('%(#1n', $conv) . "\n"; ?>%</td> <td width="71">$<?echo money_format('%(#1n', $eod) . ""; ?><td> <td width="71"><?=$emails;?> </td> <td width="150"><?if (!$notes==NULL){echo $notes;} else {echo " ";}?></td> </tr> <?php } $lastdate = $date; } // end while ?> </table> Any help would be appreciated. i need help with adding an integer into an array something like this $Loaded[count($Loaded)+1] = $To; Hey guys: If I some multi-dimensional arrays, like so: Code: [Select] array { array { [0]=> "Path1" [1]=> "10" } } array { array { [0]=> "Path2" [1]=> "16" } array { [0]=> "Path3" [1]=> "110" } } How can I add all of the values of key 1? I know its a foreach loop, but I can't figure it out... would it be this? hello, how would i add the values off all results in the $pamount array? Code: [Select] $result2 = mysql_query("SELECT * FROM pobook WHERE jobnumber = '$jobid'"); while($row2 = mysql_fetch_array($result2)) { $pamount=$row2['amount']; echo "<tr>"; echo "<td>" . $pamount . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>VALUE OF ALL RETURNED $pamount HERE</td>"; echo "</tr>"; I have an array of products. The array has an ID and price. I'd like to know the most efficient way of adding up all the same ID fields in the array. Code: [Select] <?php $items[] = array( 'id' => 1, 'price' => 5.99, ); $items[] = array( 'id' => 2, 'price' => 1.99, ); $items[] = array( 'id' => 3, 'price' => 2.40, ); $items[] = array( 'id' => 2, 'price' => 6.99, ); $items[] = array( 'id' => 4, 'price' => 8, ); $items[] = array( 'id' => 2, 'price' => 3, ); $items[] = array( 'id' => 3, 'price' => 1, ); function add_like_items($items) { //manipulation return $sums; } //$sums would be an array like //$sum[1] = 5.99 //$sum[2] = 11.98 //$sum[3] = 3.40 //$sum[4] = 8 ?> Anybody have suggestions on the best way to go about doing this? I have the following multidimensional array where each sub-array represents quantity[0] and item #[1]: Array ( => Array ( => 1 [1] => 12113 ) [1] => Array ( => 2 [1] => 21200 ) [2] => Array ( => 4 [1] => 10200 ) [3] => Array ( => 1 [1] => yyyy ) [4] => Array ( => 20 [1] => xxxxx ) [5] => Array ( => 5 [1] => 21200 ) I'm trying to consolidate like items. For example I need to create a new array that reflects any duplicate part numbers with a combined quantity. (ie. ... => 7 [1] => 21200 ... |