PHP - Regular Expressions And Search
i need to split words and add to a array from filter input.
i can split words with [ ] (Space character). But also i need to not split "word1 word2" with space character. for example: Filter input "word1 word2" word3 word4 my array will need to be like this after split arr[0] = word1 word2 arr[1] = word3 arr[2] = word4 can some one help me about this? By the way im sorry for my poor english Thanx Similar TutorialsOkay, I have a problem. I need to display multilple output of a file with a similar file name I.E. file1: 6002010.pdf file2: 6002010.mmddyyyy.pdf Now the first part of the file stays the same, but there is a time stamp on modified versions of the file. But in my output I need to display all the files with the 6002010. Could I use a wildcard such as 6002010* to get all the files to output on the screen? Or do I need to use a regular expression? This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=341968.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=315565.0 I have a payment form that submits its results to Authorize.net. It works on my laptop without an SSL certificate using an Authorize.net test account. But when I transfer this one file over to my GoDaddy account - which has cURL enabled and an SSL certificate - the page crashes when you submit the form results. (Actually you just get a blank page except for echo statements I added?!) If I take out my "Form Validation" block - which uses Regular Expressions - then the form runs on the server?! Someone said it might be that "cURL must have SSL enabled in the build", but that isn't the issue, because I can send data to an HTTPS connection and receive a response back?! Here is a streamlined version of my code... Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href=".css"> <style type="text/css" > someStyleHere{ font-weight: bold; } </style> </head> <body> <?php // Check Form. echo '*** Checking for Submitted Form ***<br />'; if (isset($_POST['submitted'])){ // Handle Form. echo '*** Form was Submitted ***<br />'; echo '*** Handling Form ***<br />'; // Trim all incoming data. $trimmed = array_map('trim', $_POST); // CHECK BILLING INFORMATION. echo '*** Check for Form Errors ***<br />'; // Check First Name. if (!empty($_POST['firstName'])){ if (preg_match('/^[A-Z \'.-]{2,20}$/i', $_POST['firstName'])){ $firstName = $_POST['firstName']; }else{ $errors['firstName'] = 'Must be 2-20 characters (A-Z \' . -)'; } }else{ $errors['firstName'] = 'Please enter your First Name.'; } // Determine if any errors. if (empty($errors)){ // PROCESS PAYMENT. echo '*** Processing Form ***<br />'; $post_url = "https://test.authorize.net/gateway/transact.dll"; // Output the Response Array to the screen as an HTML Numbered List. echo "<OL>\n"; foreach($response_array as $value){ echo "<LI>" . $value . " </LI>\n"; } echo "</OL>\n"; // Printe Response Code. switch($response_array[0]){ case "1": echo "Response Code: Approved"; break; case "2": echo "Response Code: Declined"; break; } // Do not re-display Payment Form!!! exit(); // ********************************************************************* }// End of PROCESS PAYMENT. }// End of HANDLE FORM. ?> <!-- HTML PAYMENT FORM --> <form id="payment" action="" method="post"> <fieldset> <legend>Billing Details</legend> <ol> <!-- First Name --> <li> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" class="text" type="text" maxlength="20" value="<?php echo $firstName; ?>" /> <?php if (!empty($errors['firstName'])){ echo '<span class="error">' . $errors['firstName'] . '</span>'; } ?> </li> </ol> </fieldset> <!-- Submit Form --> <fieldset id="submit"> <input name="submit" type="submit" value="Place Order" /> <input name="submitted" type="hidden" value="true" /> </fieldset> </form> </body> </html> With my Regular Expressions in, I get this output after submitting the form... Quote *** Checking for Submitted Form *** *** Form was Submitted *** *** Handling Form *** With NO Regular Expressions in, I get this output after submitting the form... Quote *** Checking for Submitted Form *** *** Form was Submitted *** *** Handling Form *** *** Check for Form Errors *** *** Processing Form *** post-string = x_login= and so on... Response Code: Approved (Which means the code is working...) I am at wit's end with this problem... Thanks, Debbie This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=351100.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=334128.0 Hi, I am not that good with understanding REGEX/REGEXP functions but, Could someone please let me know as how can I search through html code for a particular tag having a definite start and definite end. E.g. HTML Code below, it has definite Start of Code: [Select] <div id="ST and definite End of Code: [Select] house='Anything'></div></div></div> Code: [Select] <div id="ST12345678_st" class="r_right_col"> <div class=""></div> <div id=""><span class=""></span></div> <div class=""></div> <div class=""><span class=""></span></div> <div class=""></div> <div class=""> <div style="" id="" fname=Abhishek lname='madhani' division=8C house='Anything'> </div> </div> </div> So the HTML file which I have, has the this particular tag being repeated atleast more than 50 times in whole code. And I am planning to extract each of them seperately. So if someone could help me with REGEX to search for this particular tags in whole file, would be really helpful. Regards, Abhishek Madhani Hey all, Wondering maybe someone can help me out, I am training to be a firefighter (I am not a coder) and am writing a little script for my local volunteer fire brigade to try help em out a little and I am stuck! It doesn't do much but grab the address out of a pager message but I have searched and tried a few ways to get this and now I am tired and just can't get my head around how to do this! Let me explain, Below are 2 pager alert messages and I have a sore head of a time trying to extract the address and job details of the second message into a php string using Expressions/Regex... Here are 2 example messages: Code: [Select] 0571040 15:45:21 30-04-12 @@ALERT F546356345 THEB8 STRUC1 SMELL OF SMOKE AND ALARM OPERATING 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD //CROSSTREET2 AV M 99 A1 (429085) CTHEB CBOROS PT28 [THEB] 0571040 15:45:21 30-04-12 @@ALERT F546356345 THEB8 STRUC1 SMELL OF SMOKE AND ALARM OPERATING 4 / 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD //CROSSTREET2 AV M 99 A1 (429085) CTHEB CBOROS PT28 [THEB] You will note the second address has 4 / 900 at the start or it could say Unit 4 / 900... and this is where my issue starts! The addresses can come in the 2 different formats, I have first one sorted with the code below but this address with no 4 / 900 some road just has me and my code below stumped. The extra / is just to complex a thing for me and my brain to handle! lol Please Help! As you can see I use the first slash as the first cross street but in the second case above the first / is now a part of the address... Below is what I have so far: Code: [Select] function get_string_between2($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } $fullstring = "$rawPage"; if ( strpos($fullstring, ' STRUC1 ')!== false ) { $parsed = get_string_between2($fullstring, "STRUC1", "/"); } $input = "$parsed"; preg_match('/([^0-9]+)(.*)/', $input, $matches); $jobdet = "$matches[1]"; $jobadd = "$matches[2]"; Now this works fine for the top message and I get this as the result: $jobdet = SMELL OF SMOKE AND ALARM OPERATING $jobadd = 900 SOME ROAD SOMESUBURB $firstcrossstreet = /CROSSSTREET1 RD $secondcrossstreet = //CROSSSTREET2 AV For the second message it's all wrong with this the result: $jobdet = SMELL OF SMOKE AND ALARM OPERATING $jobadd = 4 $firstcrossstreet = / 900 SOME ROAD SOMESUBURB /CROSSSTREET1 RD $secondcrossstreet = //CROSSSTREET2 AV I know it's the / causing it but how can I make the code handle either case? Any help would be greatly appreciated! Thanks! The result pages is supposed to have pagination like google help me please
I require a page to be added to my website. This page will facilitate a refined search via Google, Bing and Yahoo search engine simultaneously , show the top 5 of each engine. Is this possible using php ? Thank's Hello, Does anyone know a tutorial I can follow to create my own search engine over the items I have in my SQL database? I find a lot of tutorials but they are all 'one word searches' which means if you type two words you will get all results that contain either word (too many hits). If the search engine tutorial displays result with AJAX even better. Thanks for help. df I am trying to check my youtube code value and see if &rel=0 has been added to it and if not added it. I suck at regular exp can someone help? I have code to search a database of members, I can search by lastname but having a little problem. I want to be able to search by lastname starting with the first letter. (ie: a or b or c or d and so on). As it is right now I can only search by first letter of last name if I add % to the search (ie: a% b% c%) will give me all members with the last names starting with the approiate letter designation. I'm not sure how to handle this, any help would be appreciated. Thanks. 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" /> <title></title> </head> <body> <center><table cellspacing="10" cellpadding="10" width="750" border="0"> <h4>Search</h4> <form name="search" method="post" action="<?php $PHP_SELF?>"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="lname">Last Name</option> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php // check to see if anything is posted if (isset($_POST['find'])) {$find = $_POST['find'];} if (isset($_POST['searching'])) {$searching = $_POST['searching'];} if (isset($_POST['field'])) {$field = $_POST['field'];} //This is only displayed if they have submitted the form if (isset($searching) && $searching=="yes") { echo "<h4>Results</h4><p>"; // If they did not enter a search term we give them an error if (empty($find)) { echo "<p>You forgot to enter a search term"; exit; } // Otherwise we connect to our Database mysql_connect("localhost", "root", "1910") or die(mysql_error()); mysql_select_db("cmc_member") or die(mysql_error()); // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); // Now we search for our search term, in the field the user specified $results = mysql_query("SELECT * FROM members WHERE upper($field) LIKE'$find'"); // And we display the results if($results && mysql_num_rows($results) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($results)) { // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product ALIGN='CENTER' if($fname != "" && $fname != null) echo "<td ALIGN='CENTER'><FONT COLOR='red'><b>$fname $lname</b></FONT><br> $address <br> $city $state $zip <br>$phone<br><a href=\"mailto: $email\">$email</a><br></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches = mysql_num_rows($results); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> </tr> </table></center> </body> </html> I am developing a intranet forum in Php and MySQL and I am using ajax to display searched results on the same page but right now I am using query LIKE text% to search in database which is slower. but I want to make it fast search engin which can parse *,+ and show result. Since I am using ajax i am not able to use free search engin,so if possible pls provide a complete solution Hello, I'm developing a website that asks the user to submit a keyword for a search. The results should display matches for this keyword, but also show matches for related keywords (in order of relevenace!). I'm planning on building up a library of which search terms users use in the same sessions (e.g. if someone searches for "it jobs" and "php jobs", I'll know the terms are correlated), and I'll also measure the click-through rates of the items on the results list. I've been spending all weekend trying to map out the design for this but it's proving incredibly complicated and I think the solution is likely to be on the internet somewhere already?! Please could someone point me in the right direction if you've come accross this problem before? Thanks a million, Stu Hi all How do I modify the below code to search multiple tables in mySQL database? $query = "select * from store_items where description like \"%$trimmed%\" or title like \"%$trimmed%\" or dimensions like \"%$trimmed%\" order by id ASC"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); It is for a search function and I need it to search another table called 'about_text' and 'faq_text' Thanks Pete Friends, I want to extract the Search Keyword from the URL, a visitor came from. I am using a PHP CMS and want to show the Keyword on my Blog. So if they search "abcd" from google, i want to extract "abcd" and echo on my blog. Here is the coding i could got hold of, but its not working, not echoing anything <?php function pk_stt2_function_get_delimiter($ref) { $search_engines = array('google.com' => 'q', 'go.google.com' => 'q', 'images.google.com' => 'q', 'video.google.com' => 'q', 'news.google.com' => 'q', 'blogsearch.google.com' => 'q', 'maps.google.com' => 'q', 'local.google.com' => 'q', 'search.yahoo.com' => 'p', 'search.msn.com' => 'q', 'bing.com' => 'q', 'msxml.excite.com' => 'qkw', 'search.lycos.com' => 'query', 'alltheweb.com' => 'q', 'search.aol.com' => 'query', 'search.iwon.com' => 'searchfor', 'ask.com' => 'q', 'ask.co.uk' => 'ask', 'search.cometsystems.com' => 'qry', 'hotbot.com' => 'query', 'overture.com' => 'Keywords', 'metacrawler.com' => 'qkw', 'search.netscape.com' => 'query', 'looksmart.com' => 'key', 'dpxml.webcrawler.com' => 'qkw', 'search.earthlink.net' => 'q', 'search.viewpoint.com' => 'k', 'mamma.com' => 'query'); $delim = false; if (isset($search_engines[$ref])) { $delim = $search_engines[$ref]; } else { if (strpos('ref:'.$ref,'google')) $delim = "q"; elseif (strpos('ref:'.$ref,'search.atomz.')) $delim = "sp-q"; elseif (strpos('ref:'.$ref,'search.msn.')) $delim = "q"; elseif (strpos('ref:'.$ref,'search.yahoo.')) $delim = "p"; elseif (preg_match('/home\.bellsouth\.net\/s\/s\.dll/i', $ref)) $delim = "bellsouth"; } return $delim; } /** * retrieve the search terms from search engine query * */ function pk_stt2_function_get_terms($d) { $terms = null; $query_array = array(); $query_terms = null; $query = explode($d.'=', $_SERVER['HTTP_REFERER']); $query = explode('&', $query[1]); $query = urldecode($query[0]); $query = str_replace("'", '', $query); $query = str_replace('"', '', $query); $query_array = preg_split('/[\s,\+\.]+/',$query); $query_terms = implode(' ', $query_array); $terms = htmlspecialchars(urldecode(trim($query_terms))); return $terms; } /** * get the referer * */ function pk_stt2_function_get_referer() { if (!isset($_SERVER['HTTP_REFERER']) || ($_SERVER['HTTP_REFERER'] == '')) return false; $referer_info = parse_url($_SERVER['HTTP_REFERER']); $referer = $referer_info['host']; if(substr($referer, 0, 4) == 'www.') $referer = substr($referer, 4); return $referer; } $referer = pk_stt2_function_get_referer(); if (!$referer) return false; $delimiter = pk_stt2_function_get_delimiter($referer); if( $delimiter ){ $term = pk_stt2_function_get_terms($delimiter); } echo $term; ?> May someone help? Natasha T Hello. I am working on a php script for searching a database table. I am really new to this, so I used the this tutorial http://www.phpfreaks.com/tutorial/simple-sql-search I managed to get all the things working the way I wanted, except one important and crucial thing. Let me explain. My table consist of three columns, like this: ID(bigint20) title(text) link (varchar255) ============================= ID1 title1 link-1 ID2 title2 link-2 etc... Like I said, I managed to make the script display results for a search query based on the title. Want I want it to do more, but I can't seem to find the right resource to learn how, is to place a "Download" button under each search result with its corresponding link from the table. Here is the code I used. <?php $dbHost = 'localhost'; // localhost will be used in most cases // set these to your mysql database username and password. $dbUser = 'user'; $dbPass = 'pass'; $dbDatabase = 'db'; // the database you put the table into. $con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error()); mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error()); // Set up our error check and result check array $error = array(); $results = array(); // First check if a form was submitted. // Since this is a search we will use $_GET if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 3) { $error[] = "Search terms must be longer than 3 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } // If there are no errors, lets get the search going. if (count($error) < 1) { $searchSQL = "SELECT title, link FROM db WHERE title LIKE '%{$searchTermDB}%'"; $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = "{$row['title']}<br /> Download - this is the button I want to link to the title results - and maybe other links too - <br /> "; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } ?> <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?> <form method="GET" action="search?" name="searchForm"> Search for title: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /> <input type="submit" name="submit" value="Search" /> </form> <?php echo (count($results) > 0)?"Rezultate lucrari de licenta sau disertatie pentru {$searchTerms} :<br /><br />" . implode("", $results):""; ?> $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = "{$row['title']}<br /> Download - this is the button I want to link to the title results - and maybe other links too - <br /> "; $i++; I would like the results to be displayed like this Results for SearchItem: Result 1 Download | Other link Result 2 Download | Other link etc.... or something like this. So, how do I add the data from the link row into a text(Dowload), within an <a href> tag (well, at least I guess it would go this way) ? My first tries (fueled by my lack of knowledge) where things like $results[] = "{$row['title']}<br /> <a href="{$row['link']}">Download</a> <br /> "; but I keep getting lots of errors, and then I don't know much about arrays and stuff (except basic notions); So there it is. I am really stuck and can't seem to find any workaround for this. Any suggestions? (examples, documentation, anything would do, really) Thanks, Radu Hey I'm pretty new to regular expression, so I might have made a silly mistake. I want to check if there are any symbols/special characters (not letters or numbers) in a string. I have the code below. function validateKeywords($kw) { if (preg_match("/[^A-Za-z0-9]/", $kw)) { echo 'validation of keywords failed<br />'; return false; // String contains symbols (not letters or numbers) - incorrect format! } else { echo 'validation of keywords succeeded<br />'; return true; // Correct format } } // Testing validateKeywords('some random string I wrote for testing purposes'); // should succeed validateKeywords('some random 10 string I wrote for testing purposes 2010'); // should succeed validateKeywords('some random, string, I wrote for 19 testing purposes'); // should fail validateKeywords('some, random, string I wrote, for testing purposes'); // should fail The problem is that all 4 calls return false and print that the validation failed. So, where did I mess up? Thanks! Code: [Select] <pre> <pre class="brush:cpp"> <pre class="brush:javascript"> Those can be cpp,javascript,python ... etc. I need to change/replace these into: Code: [Select] <pre> <pre class="brush:cpp"> <pre class="brush:javascript"> Those can be cpp,javascript,python ... etc. I need a generic regular expression for this? Can anyone provide the generic regular expression for this to replace? Trying=> $abc = str_replace(array($generic-regular-expression),'<pre> or <pre class="brush:cpp"> or <pre class="brush:javascript"> ..... ',$str); |