Jump to content

Delimiter must not be alphanumeric or backslash


mck.workman

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");
}
?>

Link to comment
Share on other sites

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");
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.