PHP - Preg_replace Exact Match
im trying to preg_replace the word product in a string, but if i use something like ..
$k = 'product'; $str = 'product and product.test' preg_replace("/\b$k\b/", 'replace_with_me', $str); it will replace product and product.test instead of just product. Similar TutorialsI have a array of file path strings and trying to search the array for a string that would be the end of the path sting and return the index of the file path that contains the string. I've tried using something like this, but it finds the keyword in anyplace in the file path. I've also tried strripos() as well with the same results. Any suggestions? function array_search_partial($arr, $keyword) { foreach ($arr as $index => $string) { if(strpos($string, $keyword) !== FALSE) { return $index; } } }
Is it possible to write to an exact line and column of a text file with PHP like with fwrite or something? Dear php freaks, In my test file the following code works perfectly: Code: [Select] <!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" /> <script language="javascript" type="text/javascript"> function dropdownlist(listindex) { document.formname.subcategory.options.length = 0; switch (listindex) { case "1" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); break; case "2" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); break; case "3" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); break; case "4" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); document.formname.subcategory.options[5]=new Option("4","4"); break; case "5" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); document.formname.subcategory.options[5]=new Option("4","4"); document.formname.subcategory.options[6]=new Option("5","5"); break; case "6" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); document.formname.subcategory.options[5]=new Option("4","4"); document.formname.subcategory.options[6]=new Option("5","5"); document.formname.subcategory.options[7]=new Option("6","6"); break; case "7" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); document.formname.subcategory.options[5]=new Option("4","4"); document.formname.subcategory.options[6]=new Option("5","5"); document.formname.subcategory.options[7]=new Option("6","6"); document.formname.subcategory.options[8]=new Option("7","7"); break; case "8" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); document.formname.subcategory.options[5]=new Option("4","4"); document.formname.subcategory.options[6]=new Option("5","5"); document.formname.subcategory.options[7]=new Option("6","6"); document.formname.subcategory.options[8]=new Option("7","7"); document.formname.subcategory.options[9]=new Option("8","8"); break; case "9" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); document.formname.subcategory.options[5]=new Option("4","4"); document.formname.subcategory.options[6]=new Option("5","5"); document.formname.subcategory.options[7]=new Option("6","6"); document.formname.subcategory.options[8]=new Option("7","7"); document.formname.subcategory.options[9]=new Option("8","8"); document.formname.subcategory.options[10]=new Option("9","9"); break; case "10" : document.formname.subcategory.options[0]=new Option("Select kinderen thuis",""); document.formname.subcategory.options[1]=new Option("0","0"); document.formname.subcategory.options[2]=new Option("1","1"); document.formname.subcategory.options[3]=new Option("2","2"); document.formname.subcategory.options[4]=new Option("3","3"); document.formname.subcategory.options[5]=new Option("4","4"); document.formname.subcategory.options[6]=new Option("5","5"); document.formname.subcategory.options[7]=new Option("6","6"); document.formname.subcategory.options[8]=new Option("7","7"); document.formname.subcategory.options[9]=new Option("8","8"); document.formname.subcategory.options[10]=new Option("9","9"); document.formname.subcategory.options[11]=new Option("10","10"); break; } return true; } </script> </head> <title>Dynamic Drop Down List</title> <body> <form id="formname" name="formname" method="post" action="submitform.asp" > <table width="50%" border="0" cellspacing="0" cellpadding="5"> <tr> <td width="41%" align="right" valign="middle">Aantal kinderen :</td> <td width="59%" align="left" valign="middle"><select name="category" id="category" onchange="javascript: dropdownlist(this.options[this.selectedIndex].value);"> <option value="">------</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select></td> </tr> <tr> <td align="right" valign="middle">Waarvan thuiswonend : </td> <td align="left" valign="middle"><script type="text/javascript" language="JavaScript"> document.write('<select name="subcategory"><option value="">-----</option></select>') </script> <noscript><select name="subcategory" id="subcategory" > <option value="">Select Sub-Category</option> </select> </noscript></td> </tr> </table> </form> </body> </html> When i want to implement it in my signup page, it doesnt do anything. I've checked it a thousand times, i double checked that also the -formname of my signup page is 'formname' -the element name stays 'subcategory' I really need this to be implemented. Where can be the problem? What should i look for? Hello everyone, I am trying to select and return any rows that have a specific string in it. Let's say I have a column name "item_id" and in every row of that column I have values like so: row A: (11,22,44,34) row B: (12,56,78,98) If the user picks the number '22', I use the following SQL line to get the row in which the number '22' is contained. SELECT * FROM Persons WHERE City LIKE '%22%' and row A gets returned, just like it is suppose to. The problem is, if the user selects a single digit number like '2', both row A and row B gets returned because they both have values that contain the number '2' in it. Instance, row A has the number '22' and row B has the number '12' so both rows get returned when the number '2' is requested. I don't want that, I only want the row that has the exact number '2' or '34' or whatever number I request, to be returned. So to get around this problem, I am wrapping my numbers in letters like so: row A: (a11a,a22a,a44a,a34a) row B: (a12a,a56a,a78a,a98a,a2a) and requesting it like so: SELECT * FROM Persons WHERE City LIKE '%a22a%' My question is: Is there a better way to work out this logic? If I query my database:
www.mydomain.com/index.php?room=M210
I get both room M201 and M210B in my results.
I know enough to know that is a result of:
and (allstate_schedule.room LIKE '$room%')So I change it to this: and (allstate_schedule.room LIKE '$room')Now the query works correctly: www.mydomain.com/index.php?room=M210 But if I pull up an unfiltered list: www.mydomain.com/index.php ... I get my "No records found matching:" here is my code: $query = "SELECT allstate_schedule.sponsor,allstate_schedule.school,allstate_schedule.student,allstate_schedule.instrument,allstate_schedule.school,allstate_schedule.room,allstate_schedule.time from allstate_schedule WHERE ((allstate_schedule.sponsor LIKE '$sponsor%') and (allstate_schedule.student LIKE '%$student%') and (allstate_schedule.instrument LIKE '$instrument%') and (allstate_schedule.school LIKE '$school%') and (allstate_schedule.room LIKE '$room%') and (allstate_schedule.time LIKE '$time%')) ORDER BY allstate_schedule.$sort LIMIT $eu, $limit "; $resultt = mysql_query($query); // Determine the number of employees $number = mysql_num_rows($resultt); if ($number == 0) { print "<b>No records found matching</b>:"; $sponsor = $sponsor; $school = $school; $room = $room; $student = $student; Edited by wkilc, 08 November 2014 - 12:07 PM. is there a way to print the file as it appears. I want to print the HTML and PHP within a file rather than just the output. Example: File contains: <?php echo "Hello World"; ?> i would like to echo: <?php echo "Hello World"; ?> instead of just hello world which is what i get with file_get_contents() 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); ?> I’m translating some mixed PHP/MySQL code into pure MySQL code. Now I'm having a problem about how to translate PHP's time() function. I’m not sure how to choose between NOW(), CURDATE(), CURTIME(), and several others. I tried NOW(), but to my surprise it outputs today’s year (i.e. 2014) instead of the Unix integer timestamp I'm looking for. And this behaviour seems contray to the MySQL manual. What should I do ? :-\Have been reading a few places about how to set php.ini file to be able to use MySQL on my Yahoo website. But am also warned to avoid tampering needlessly with registry. So I need an absolute precedure, with port 465, I also know. Embarassing how long this has taken me to find some of this out! Thanks, Yshua Hi Im working on a market place style website. the framework im using is called elgg. it uses an algoritm to find the right approximate width and height of an image thats being uploaded. but on my site, all heights and widths must be the same. this is difficult because some pictures have much greater width than height and some have much greater height. so whats the best algoritm to use to make an accurate thumbnail of all images uploaded? Thanks err, dunno if i managed to put it in the right thread but if i didn't, please tell me. thank you.
getting back on topic, my father asked me to create a website for a school. i know how to make a website but the problem is, how do i start? should i ask about their objectives or something? color of the webpage etc? in short, i don't know the practicalities and processes used in making a website for a certain client. you could say this would be my first time in doing this (kind of like an on-the-job training) so...any ideas? i would very much appreciate any help given.
I currently have some custom tags, similar to BBCode, to be used for comments on my site. I'm trying to create a [nobr] tag (note: I know the no break tag will work in many major browsers, but it's deprecated, so I'm trying to be compliant). The idea is that if I encounter the [nobr] tag, I need to remove all line breaks. Here's the best I can do, but it's not working as I had hoped. Here's what I currently use to replace line breaks with a break tag. Code: [Select] $str=str_replace("\r\n","<br>",$str); Here's my attempt at introducing this new tag. Code: [Select] function BbToHtml($str) { $before=preg_replace("/(.+)\[nobr\](.+)\[\/nobr\](.+)/Usi","\\1",$str); $during=preg_replace("/(.+)\[nobr\](.+)\[\/nobr\](.+)/Usi","\\2",$str); $after=preg_replace("/(.+)\[nobr\](.+)\[\/nobr\](.+)/Usi","\\3",$str); $during=str_replace("\r\n","",$during); if($during != "") { // If the [nobr] tag was found, go with the new string. $str = $before.$during.$after; } else { // If the [nobr] tag was not found, go ahead like normal. $str=str_replace("\r\n","<br>",$str); } return $str; I can't get that working. Any ideas on how to fix that, or a completely alternate method of doing it? Thank you! Hello ppl, I have this website link http://www.somesite.com/anotherpage/yetotherpage.html what i want to make is using preg_replace that the final result will be www.somesite.com I'm trying to search a string for all occurences of a pattern, then append text after that pattern. The example is:
SUM("foo") as "bar"
and have it display as:
SUM("foo") over() as "bar"
The "SUM" could be any SQL aggregate and be case insensitive. So For bonus points, let's say the valid aggregates are array('SUM', 'MAX', 'MIN', 'AVG'). My plan right now is to figure it out for SUM, then loop it replacing the aggregate.
This is what I have so far... and it is not working.
$l= 'SUM("foo") as "bar"';
$l=preg_replace('/([^"]+")/','\0 over()',$l);
Thanks,
Ryan
After days of searching I found this thread http://www.phpfreaks.com/forums/index.php?topic=236954.0 that helped me do what I was trying to do. I've gotten the code to create and rename my html document but I can't figure out how to use the preg_replace part. This is my template with the 3 variables I want to replace with values from a form. Code: [Select] <html> <head> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <?php include_once('content.php') ?> </body> <SCRIPT language="JavaScript" SRC="javascript.js"></SCRIPT> <script type="text/javascript"> function nametitle(){ parent.iframe1.document.getElementById("div1").innerHTML = "VARIABLE1 <br>VARIABLE2"; parent.iframe1.document.getElementById("quotes").innerHTML = "VARIABLE3"; } </script> </html> I've spent hours reading and trying to understand how to make it work but it's a little over my head. Could someone help me with this? Thanks, Hi there! I need to echo some text from a database table, this text should be linked to a url stored in the same database. I am trying to do this with preg_replace but I am terrible at it! I keep getting this error: Warning: Wrong parameter count for preg_replace() while($row = mysql_fetch_array( $result )) { $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; echo preg_replace ($reg_exUrl, $row['title'); Any suggestion? Thanks I am trying to pull out the image HTML tag but its not working as expected. Here is what I am doing $removeimagetag='/<a href/'; $replacwith = "illegal"; $_POST['descr']=preg_replace($removeimagetag,$replacwith,$_POST['descr']); in the 'descr' there is a <a href="http://s635.photobucket.com...... When it post it does not strip out the <a href So the image is displayed. I tried a modified version to test to see if it work that looked like this. $removeimagetag='/goofy/'; $replacwith = "illegal"; $_POST['descr']=preg_replace($removeimagetag,$replacwith,$_POST['descr']); When the word goofy appears in the text then its replaced with the word illegal, Also I would like to make it so that it replaces the entire line from the <a href....... to the </a> with a phrase that says "this feature is not allowed" rather than just the part in the front...the <a href. Any help you can be will be greatly appreciated. i have: $msg = ereg_replace("[^A-z0-9]"," ",$msg); $msg = ereg_replace(" "," ",$msg); $msg = ereg_replace(" "," ",$msg); how will i apply preg_replace please??? thank you Hi, I'm kind of new to PHP and very new to Regular Expressions and I was wondering if someone could help run through this code snippet from a callback function in a Joomla plugin called Affliates Feed. So far I have this is passed a pointer to variable item (I understand this is effectively the same as passing the variable but a lot less CPU intensive. Creates an array with keys and values Now the for loop I am stuck with, I understand the interation through the array. But what is preg_replace returning and why pass it back that way hasn't it already done its job? Can any one help? And please feel free to be as basic as possible :help: Thanks, Adam Code: [Select] function gardening(&$item) { #renaming categories, not the most efficient use for example strtolower first, and use only lowercase in the rename array $rename=array( ' Gardening?' => '', 'Climber Plants' => 'Climbing Plants', 'Seeds.*Bulbs' => 'Seeds Bulbs', 'Plant protection' => 'Horticultural goods', 'Garden structures' => 'Garden buildings',); foreach ( $rename as $k => $v ) { list($item['menu_1'],$item['menu_2'])=preg_replace("#$k#",$v,array($item['menu_1'],$item['menu_2'])); } $item['menu_1']=ucfirst(strtolower($item['menu_1'])); $item['menu_2']=ucfirst(strtolower($item['menu_2'])); $item['menu_3']=ucfirst(strtolower($item['menu_3'])); $item['menu_4']=ucfirst(strtolower($item['menu_4'])); } im trying to do a complex(i think) prg replace but i keep getting the error: Code: [Select] Failed evaluating code: $lang->showing-1,1 the code i am trying to replace is: Code: [Select] <asf: lang[showing-<asf: var[num_report_alerts]>,<asf: var[num_report_alerts]>]> and the preg_replace is: Code: [Select] $content = preg_replace('|\<asf: lang\[(.*?)-(.*?)\]\>|e', '$lang->replace_vars($lang->//1, array(\\2))', $content); its supposed to take everything inside <asf: lang[]> and echo \\1 which would be "showing" and then add \\2 into an array which would be "array(1,1)". So the output would be showing 1 of 1. Anyone know where im going wrong? |