mck.workman Posted January 15, 2012 Share Posted January 15, 2012 Hey guys! I am trying to find matches to discussions in a text file and am getting the error Warning: preg_match_all() [function.preg-match-all]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\mywebsite\temp1.php on line 6 Cant write to file <?PHP if(isset($_POST['Store'])) { $mainUrl = 'http://www.grasshopper3d.com/forum/categories/sample-and-example-files/listForCategory'; //search for all URLs with www.grasshopper3d/forum/topics/bla preg_match_all("discussion.", $mainUrl, $urlMatches); $fileName = "urls.txt"; $urlFile = fopen($fileName, 'w') or die("Cant open file"); fwrite($urlFile, $urlMatches) or die("Cant write to file"); } ?> Can someone please help me figure out why? Quote Link to comment Share on other sites More sharing options...
joe92 Posted January 15, 2012 Share Posted January 15, 2012 You need delimiters round your pattern, turn this preg_match_all("discussion.", $mainUrl, $urlMatches); into this preg_match_all("/discussion./", $mainUrl, $urlMatches); A delimiter (the '/'s in this case) tells the regex engine what the boundaries of the pattern are. They are compulsory for all regex patterns. However, do note that this will return no matches. That is going to look for a match of 'discussion.' in the url string that you have provided. Quote Link to comment Share on other sites More sharing options...
abareplace Posted January 16, 2012 Share Posted January 16, 2012 Also note that "/discussion./" matches discussion and any character, not discussion and a dot. You need to escape the dot: "/discussion\./" Quote Link to comment Share on other sites More sharing options...
mck.workman Posted January 18, 2012 Author Share Posted January 18, 2012 Okay. Thank you. When I run the code and have it echo an item in the array it only prints 'Array' to the screen. I tried converting it to a string via implode and accessing the array with an index but it still just prints 'Array' to the screen. <?PHP if(isset($_POST['Store'])) { $mainUrl = 'http://www.grasshopper3d.com/forum/categories/sample-and-example-files/listForCategory'; //search for all URLs with www.grasshopper3d/forum/topics/bla preg_match_all("/discussion./", $mainUrl, $urlMatches); $fileName = "urls.txt"; $urlFile = fopen($fileName, 'w') or die("Cant open file"); echo('matches: '. $urlMatches[0]); //fwrite($urlFile, $urlMatches) or die("Cant write to file"); } ?> Quote Link to comment Share on other sites More sharing options...
joe92 Posted January 18, 2012 Share Posted January 18, 2012 Preg_match_all prints it's results into a multidimensional array. To get the results you need to print out position[0][0] for the first result and position[0][1] for the second etc.. Also, what abareplace said was right, that was awfully sloppy of me. The dot will match any character except a newline so you need to escape it, my mistake. This should fix it: <?php if(isset($_POST['Store'])) { $mainUrl = 'http://www.grasshopper3d.com/forum/categories/sample-and-example-files/listForCategory'; //search for all URLs with www.grasshopper3d/forum/topics/bla preg_match_all("/discussion\./", $mainUrl, $urlMatches); //escape the dot $fileName = "urls.txt"; $urlFile = fopen($fileName, 'w') or die("Cant open file"); echo('matches: '. $urlMatches[0][0]); //multidimensional array so print position [0][0] //fwrite($urlFile, $urlMatches) or die("Cant write to file"); } ?> Quote Link to comment Share on other sites More sharing options...
mck.workman Posted January 19, 2012 Author Share Posted January 19, 2012 Got it! Thank you so much! McK Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.