PHP - Replacing Multiple Numbers With Words
I've been trying to go around with str_replace to replace numbers with words.
I have a database which has table actions. The actions are saved in numbers from 0 to 20. I'd need to get something to replace the numbers with words to explain what actions these are. I've tried using this. $action1 = str_replace("5", "Command", "action1"); But that doesn't work. Not even 100% sure if that is even correct, because I am new to the str_replace thing anyway. Could someone help me with this, please? Similar TutorialsHi All Again I am building a job site with a CV match system. Part of the match is education. I have a number of select boxes in a form for the number of qualifications for each individual applicant. What I would like to know is what is the correct way to put this into a MySQL database and I do not mean the insert code! i.e. English Grade 3. Maths Grade 2. I am getting them to put in total number of grades over 1, then 2 etc. When the query is made I would like the recruiter have the option of not just selecting the grades but subject with it. Should I put English then a , then the grade? It needs to be set up so I can run the query. Thanks for looking and as per last request thanks for your time and help just a newbie in php i got the code in this forum and it really works, but not that exact, it can really compare word from array to per line in a file. but what im trying to do is, to compare the exact word/number. if(strstr($line,$key)) echo $line; if $key = 123 and line 1 is 1234 and line 2 is 123 it will still output line1 Code: [Select] $key = "waka"; $fc = file("file.txt"); $f = fopen("file.txt","r"); foreach($fc as $line) { if(strstr($line,$key) echo 4line; } fclose($f); ?> Hello all! I'm having some issues with a snippet that I found online and have edited to how I want it. Basically the code works at the moment, except if I were to search for 2 or 3 terms, it would search the columns username/action/result/changes for any result that have any of the words rather than results that include both terms, instead of either. I want the query to be like (assuming 2 search terms): Quote username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' AND username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' and not: Quote username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' OR username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%' It needs an AND instead of an OR, but I'm not sure how to go around doing it. Here's the code as it is at the moment: $filter = ($_POST['filter']); $query = "SELECT * FROM activity WHERE"; $searchresult = explode(" ", $filter); foreach ($searchresult as $key => $filter) { $query .= " username LIKE '%$filter%' OR action LIKE '%$filter%' OR result LIKE '%$filter%' OR changes LIKE '%$filter%'"; if ($key != (sizeof($searchresult) - 1)) $query .= " AND "; } $query .= "ORDER BY id DESC"; Sorry if this is a little confusing to understand, hope someone understands what I need. does anyone have a code todo this? a snippet or soemthing ive tried a few diffrent ones ive seen but nothing that works.. it takes whats submited in search, then takes the words, breaks them up and creates a query string with all the diffrent feilds... here is what phpadmin produces when i search SELECT * FROM `breedclads`.`activeamahorse` WHERE (`id_entered` LIKE '%green%' OR `uuid` LIKE '%green%' OR `ama_hr_coat` LIKE '%green%' OR `breedable` LIKE '%green%' OR `ama_hr_sex` LIKE '%green%' OR `ama_hr_age` LIKE '%green%' OR `date_entered` LIKE '%green%' OR `ama_hr_eyes` LIKE '%green%' OR `ama_hr_eyes_type` LIKE '%green%' OR `ama_hr_mane` LIKE '%green%' OR `ama_hr_tail` LIKE '%green%' OR `ama_hr_luster_coat` LIKE '%green%' OR `ama_hr_luster_hair` LIKE '%green%' OR `ama_hr_gleam_coat` LIKE '%green%' OR `ama_hr_gleam_hair` LIKE '%green%' OR `ama_hr_parents` LIKE '%green%' OR `ama_hr_sale_type` LIKE '%green%' OR `ama_hr_sale_price` LIKE '%green%' OR `ama_hr_location` LIKE '%green%' OR `auction_date` LIKE '%green%' OR `picture_type` LIKE '%green%' OR `picture_name` LIKE '%green%' OR `picture_size` LIKE '%green%' OR `picture_content` LIKE '%green%' OR `ama_hr_coat_type` LIKE '%green%' OR `ama_hr_coat_gloom` LIKE '%green%' OR `ama_hr_hair_gloom` LIKE '%green%' OR `ama_hr_slurl` LIKE '%green%' OR `class_link_key` LIKE '%green%') AND (`id_entered` LIKE '%albino%' OR `uuid` LIKE '%albino%' OR `ama_hr_coat` LIKE '%albino%' OR `breedable` LIKE '%albino%' OR `ama_hr_sex` LIKE '%albino%' OR `ama_hr_age` LIKE '%albino%' OR `date_entered` LIKE '%albino%' OR `ama_hr_eyes` LIKE '%albino%' OR `ama_hr_eyes_type` LIKE '%albino%' OR `ama_hr_mane` LIKE '%albino%' OR `ama_hr_tail` LIKE '%albino%' OR `ama_hr_luster_coat` LIKE '%albino%' OR `ama_hr_luster_hair` LIKE '%albino%' OR `ama_hr_gleam_coat` LIKE '%albino%' OR `ama_hr_gleam_hair` LIKE '%albino%' OR `ama_hr_parents` LIKE '%albino%' OR `ama_hr_sale_type` LIKE '%albino%' OR `ama_hr_sale_price` LIKE '%albino%' OR `ama_hr_location` LIKE '%albino%' OR `auction_date` LIKE '%albino%' OR `picture_type` LIKE '%albino%' OR `picture_name` LIKE '%albino%' OR `picture_size` LIKE '%albino%' OR `picture_content` LIKE '%albino%' OR `ama_hr_coat_type` LIKE '%albino%' OR `ama_hr_coat_gloom` LIKE '%albino%' OR `ama_hr_hair_gloom` LIKE '%albino%' OR `ama_hr_slurl` LIKE '%albino%' OR `class_link_key` LIKE '%albino%') AND (`id_entered` LIKE '%69%' OR `uuid` LIKE '%69%' OR `ama_hr_coat` LIKE '%69%' OR `breedable` LIKE '%69%' OR `ama_hr_sex` LIKE '%69%' OR `ama_hr_age` LIKE '%69%' OR `date_entered` LIKE '%69%' OR `ama_hr_eyes` LIKE '%69%' OR `ama_hr_eyes_type` LIKE '%69%' OR `ama_hr_mane` LIKE '%69%' OR `ama_hr_tail` LIKE '%69%' OR `ama_hr_luster_coat` LIKE '%69%' OR `ama_hr_luster_hair` LIKE '%69%' OR `ama_hr_gleam_coat` LIKE '%69%' OR `ama_hr_gleam_hair` LIKE '%69%' OR `ama_hr_parents` LIKE '%69%' OR `ama_hr_sale_type` LIKE '%69%' OR `ama_hr_sale_price` LIKE '%69%' OR `ama_hr_location` LIKE '%69%' OR `auction_date` LIKE '%69%' OR `picture_type` LIKE '%69%' OR `picture_name` LIKE '%69%' OR `picture_size` LIKE '%69%' OR `picture_content` LIKE '%69%' OR `ama_hr_coat_type` LIKE '%69%' OR `ama_hr_coat_gloom` LIKE '%69%' OR `ama_hr_hair_gloom` LIKE '%69%' OR `ama_hr_slurl` LIKE '%69%' OR `class_link_key` LIKE '%69%') i need this to be made when people search with mutliple words as you can see it searchs each feilds for the green then each feild for albino, so on.. but i wont ever know what there searching for Please help me, thanks I have a search where I want to be able to search a string of words. The search is going to be looking in 2 different table joined by a left outer join. The tables are "quotes" and "categories". $sql="SELECT q.id, q.username, q.quote, q.by, q.voteup, q.votedown, q.servtime, c.quote_id, c.label FROM quotes q LEFT OUTER JOIN categories c ON q.id = c.quote_id WHERE ( q.username LIKE '$srch' OR q.quote LIKE '$srch' OR q.`by` LIKE '$srch' OR c.label LIKE '$srch')"; The above mysql statement works and returns values..BUT if say, I search "john" and "funny", and a quote is posted by the user "john", but has a category of "funny" it will output the same quote twice. I was wondering if there was a way to see if a quote has either 1 term or both terms, if so display that quote but only display it once. Below is what the query is outputting. [100] => Array ( [id] => 100 [username] => John [quote] => new test quote blah blah [by] => John [voteup] => 0 [votedown] => 0 [servtime] => 2010-12-02 @ 16:27:03 [label] => Array ( [0] => Historic [1] => Serious [2] => Funny ) ) Here is the code in full. //// $sword = explode(" ",$search); foreach($sword as $sterm){ $srch="%".$sterm."%"; echo"$srch<br />"; $sql="SELECT q.id, q.username, q.quote, q.by, q.voteup, q.votedown, q.servtime, c.quote_id, c.label FROM quotes q LEFT OUTER JOIN categories c ON q.id = c.quote_id WHERE ( q.username LIKE '$srch' OR q.quote LIKE '$srch' OR q.`by` LIKE '$srch' OR c.label LIKE '$srch')"; $result=mysql_query($sql); while($row=mysql_fetch_object($result)){ $quote[$row->id]['id'] = $row->id; $quote[$row->id]['username'] = $row->username; $quote[$row->id]['quote'] = $row->quote; $quote[$row->id]['by'] = $row->by; $quote[$row->id]['voteup'] = $row->voteup; $quote[$row->id]['votedown'] = $row->votedown; $quote[$row->id]['servtime'] = $row->servtime; $quote[$row->id]['label'][] = $row->label; } echo"<pre>"; print_r($quote); echo"</pre>"; I don't think this is the fastest way of doing this, as it loops for each item in the db, per each keyword that is search. Any help would be great!! -BaSk I found this regex, which is what I want, here:
(?=.*\bdog\b)(?=.*\bpuppet\b)(?=.*\bfuzzy\b)It is suppose to 'succeed' if it can find all the given words in any order. Yet, when I try to test it against this sequence of words (or any other order of these words): need fuzzy hand puppet in dogpen for dogI get no indication of a match. What am I not understanding? First off, hello all! I want to improve our website's search but unfortunately don't know enough to get there! I'm not a PHP programmer but I'm generally able to tweak things... Our search page has 3 options - All words, Any word, Exact. I want to concentrate on the default "All words" which is the default parameter. The code is essentially as follows - Code: [Select] $gotcriteria=TRUE; $Xstext = mysql_escape_string($stext); $aText = split(" ",$Xstext); $aFields[0]="products.pId"; $aFields[1]="products.pName"; $aFields[2]="products.pDescription"; $aFields[3]="products.pLongDescription"; if($stype=="exact") $sSQL .= "AND (products.pId LIKE '%" . $Xstext . "%' OR ".getlangid("pName",1)." LIKE '%" . $Xstext . "%' OR ".getlangid("pDescription",2)." LIKE '%" . $Xstext . "%' OR ".getlangid("pLongDescription",4)." LIKE '%" . $Xstext . "%') "; else{ $sJoin="AND "; if($stype=="any") $sJoin="OR "; $sSQL .= "AND ("; for($index=0;$index<=3;$index++){ //$sSQL .= "("; $rowcounter=0; $arrelms=count($aText); foreach($aText as $theopt){ if(is_array($theopt))$theopt=$theopt[0]; $sSQL .= $aFields[$index] . " LIKE '%" . $theopt . "%' "; if(++$rowcounter < $arrelms) $sSQL .= $sJoin; } //$sSQL .= ") "; if($index < 3) $sSQL .= "OR "; } $sSQL .= ") "; } Here's the important bit - as far as I can see, the search looks at each of the 4 fields in the array individually? So if the search text is "RED APPLE", to get a result for the "All Words" option will require that both "RED" AND "APPLE" will need to be present in one of the searched fields...? What I want to achieve is that if the product name is "RED FRUIT" and the description mentions "APPLE", then an "All words" search for "RED APPLE" will pick this up. I'd guess that the easiest way would be to string the table fields together for searching but I don't know how to do this as a query... now I'm thinking I'll get flamed for not posting this in the MySQL forum too!! Thanks in advance for any help! Hi there I am beginning to learn regex in php. I was wondering why the following regex pattern wasn't finding the two words in the string and replacing them with welcome? '/(Hello).*?(Hi)/is' Code: [Select] $patterns = array(); $patterns[0] = '/(Hello).*?(Hi)/is'; $replacements = array(); $replacements[0] = 'Welcome'; ksort($patterns); ksort($replacements); echo preg_replace($patterns, $replacements, $string); Any help would be much appreciated. Thanks a million! I am writing a stock program and I am going to be getting several averages on volume over different periods, 10, 21, 50,100,120,150,200,240 are my averages.
I would like to make this fairly efficient, seeing as how its going to be a ton of data, I have over 7700 stocks in the database and they will each have all of those averages.
My question is how to do this that is the most efficient, I thought about taking the 10 day then adding 11 to it, then adding 29 to get the 50 day and so on and so forth, or is that too much work and I should just do each average individually?
select avg(volume) from _AAPL where date between ‘2014-02-03’ and ‘2014-05-05’;The above is my current very generic code that does work, now I just need to make a php script, which I can do, I am just curious about how different people would approach this. Thanks for the help. Edited by illcrx, 23 September 2014 - 10:32 PM. Hi iam currently writing some code for my website and i have a number of records that i simply want to display in a table format, i have done this in a while loop and it works well, however i only want to display a maximum of 10 records per page as my div needs to be a fixed height and was wondering how i could include a next button that would act as like a page number. The thing is at any given point there could be from 1 record to display to 500, so i need a way of allowing the user to quickly navigate to the other records. How can i do this without using mulitple pages. Hope this makes sense. Heres my code Code: [Select] <?php echo"<table width='100%' border='1' align='center'><tr><th>Referal Name</th><th>Amount Received</tr>"; while ($row = mysql_fetch_array($sql_info)) { extract($row); echo "<tr><td align='center'>".$username."</td><td align='center'>".$cumulative_referral."</td></tr>"; } echo "</table>"; ?> Thanks for your help Hi, I need to sort variables in groups of up to 15 and put it in an array. For example: $exstract['center_tabOpBody_0'] =5 $exstract['center_tabOpBody_1'] =6 $exstract['center_tabOpBody_2'] =8 $exstract['center_tabOpBody_3'] =1 Should yield: ARRAY( = center_tabOpBody_1,center_tabOpBody_2,center_tabOpBody_3 // <-----15 [1] = center_tabOpBody_0 //<----5 ) Is there some simple function do do the "efficiency" sort? Thanks, Vadim Please help. I am trying to replace parenthesises () in a string with <>. I tried preg_replace and str_replace, but was not success. Here is what I have for str_replace: $word = "(test)"; $word = str_replace('(', '<', $word); $word = str_replace(array('(', ')'), array('<','>'), $word); hi freaks, still working on my email bot. im currently trying to escape <>-tags to be displayed- but not the browser wont exectute it (i.e. xls-tags) i want them as chars. so whin my $content is like <xsl:stylesheet> <xsl:template match="/"> <more xls...> it shows a stylesheet. i just want the chars. Here is what ive been trying so far: but its bad Code: [Select] <?php $string='<tag> > < asdasf'; $bomb=array( '<', '>', ); $defusal=array( '-', '!', ); $string= preg_replace($bomb,$defusal,$string); echo $string; ?> error: Warning: preg_replace() [function.preg-replace]: No ending matching delimiter '>' found in C:\xampp1\htdocs\code.php on line 13 donno any further now Hey all, i need a function or a way to replace each letter in a string to a star (*) Thanks Hey guys, I have a quick question. Im going to be making a script that will output the name of a news article along with the poster's name beside it. There is only a certain amount of space where that stuff can go so I want to know how I can make it so that say after 20 characters, it ends and puts "..." beside it. Is this possible with php? How would I do it? How come this is not replacing correctly? $rp = strtr($rp,"[TIMES]","<span style='font-family: Times New Roman, Times, serif'>") Hello, Is str_replace the best way to go about this? In MYSQL, a html template is stored, such that: Quote <table width="{$width}">lots of other ........</table> This is extract from the database, and stored in a string. I would use eval(), but, any user can update this template, so I need to be able to set which variables will be replaced with what the variable is. Thanks! Hi first post here and very new to php. I built a form so that a data entry perosn can "build" new pages for our companies website. I pretty much have the basics of the form and the action page coded correctly, however my issue is that I need this final page where the form inputs are posted too to be an html document. I read some about the str_replace() function, but I'm not understanding how I could use that to replace the file extension of a page. Maybe I'm going about this all wrong. I also read that one can edit the htaccess folder to allow an html file to run php script. Not sure which way would be the best. One thing to mention though, is this application would be run locally to our back up files which is then scheduled to be uploaded to the remote server very night. Any help or suggestions would be greatly appreciated. |