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?

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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.