Page 1 of 1

Warning in preg_split

Posted: Tue Sep 12, 2006 5:43 am
by nadavvin
Hello

Code: Select all

Warning: preg_split() [function.preg-split]: No ending delimiter '^' found in /var/www/blog/textToHTML.php on line 4

Warning: Invalid argument supplied for foreach() in /var/www/blog/textToHTML.php on line 5
the php code is:

Code: Select all

<?php
$result;
$regex='^|\n.*\n|$';
$array=preg_split($regex,$_post['content']);
foreach($array as $value){
	$result.="<p>$value</p>";
}
echo $result;
?>
The content which I send is:

Code: Select all

sdfgdsfg
asdfgadfg
afsdgsdfgadsfgdfsg

asdfg
asdfg
What is the problem?

Posted: Tue Sep 12, 2006 6:43 am
by feyd
I'll assume you decided to use | as your delimiter. In that case ^ and $ shouldn't be outside of them. preg_split() considered ^ as the delimiter. Since there is no matching one it threw an error.

You may want to use a pattern of

Code: Select all

$regex = '#^#m';

Code: Select all

[feyd@home]>php -r "$a = 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf'; var_dump(preg_split('#^#m', $a, -1, PREG_SPLIT_NO_EMPTY));"
array(4) {
  [0]=>
  string(6) "asdf
"
  [1]=>
  string(6) "asdf
"
  [2]=>
  string(6) "asdf
"
  [3]=>
  string(4) "asdf"
}

Posted: Tue Sep 12, 2006 6:56 am
by nadavvin
Thanks, I didn't fink that delimeter is require here.

Code: Select all

<?php
$result;
$regex='/^|\n.*\n|$/m';
$array=preg_split($regex,$_post['content']);
foreach($array as $value){
	$result.="<p>$value</p>";
}
echo $result;
?>
The result is:

Code: Select all

<p></p><p></p>
Why $value is empty?

I get the same result with your regex:

Code: Select all

$regex = '#^#m';

Posted: Tue Sep 12, 2006 7:06 am
by feyd
  1. Delimiters are always required.
  2. preg_split() removes the pattern it finds, this is the same behaviour as explode()
  3. you're using $_post not $_POST

Posted: Tue Sep 12, 2006 7:13 am
by nadavvin
I try your example but I get an error:

Code: Select all

$ php -r "$a = 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf'; var_dump(preg_split('#^#m', $a, -1, PREG_SPLIT_NO_EMPTY));"

Parse error: syntax error, unexpected '=' in Command line code on line 1

Posted: Tue Sep 12, 2006 7:18 am
by feyd
It shouldn't.

What version of php are you using?

Code: Select all

[feyd@home]>php -v
PHP 5.1.5 (cli) (built: Aug 15 2006 23:54:56)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
    with Xdebug v2.0.0beta6, Copyright (c) 2002, 2003, 2004, 2005, 2006, by Derick Rethans

[feyd@home]>php4 -r "$a = 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf'; var_dump(preg_split('#^#m', $a, -1, PREG_SPLIT_NO_EMPTY));"
array(4) {
  [0]=>
  string(6) "asdf
"
  [1]=>
  string(6) "asdf
"
  [2]=>
  string(6) "asdf
"
  [3]=>
  string(4) "asdf"
}

[feyd@home]>php4 -v
PHP 4.4.4 (cli) (built: Aug 16 2006 01:17:55)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

Posted: Tue Sep 12, 2006 7:22 am
by nadavvin
feyd wrote:
  1. Delimiters are always required.
  2. preg_split() removes the pattern it finds, this is the same behaviour as explode()
  3. you're using $_post not $_POST
It still don't work and I use in your regex:

Code: Select all

<?php
$result;
#$regex='/^|\n.*\n|$/m';
$regex = '#^#m';
$array=preg_split($regex,$_POST['content']);
foreach($array as $value){
	$result.="<p>$value</p>";
}
echo $result;
echo $array[0];
?>
What should I do for:
feyd wrote: preg_split() removes the pattern it finds, this is the same behaviour as explode()

Posted: Tue Sep 12, 2006 7:29 am
by feyd
Are you sure $_POST['content'] is what you think it is?

Code: Select all

echo '<pre>' . var_export($_POST['content'], true) . '</pre>';

Posted: Tue Sep 12, 2006 8:02 am
by nadavvin

Code: Select all

<html>
<head><meta/><title></title></head>
<body>
<?php
echo '<pre>' . var_export($_POST, true) . '</pre><br />';
$result;
#$regex='/^|\n.*\n|$/m';
$regex = '#^#m';
$array=preg_split($regex,$_POST['content']);
foreach($array as $value){
	$result.="<p>$value</p>";
}
echo $result;
echo $array[0];
?>
</body>
</html>
I don't know what is the problem, It's return nothing

Code: Select all

array (
)
the html is:

Code: Select all

<html>
<head><meta/><title></title></head>
<body>
<form action="textToHTML.php" method="post">
<textarea id="content" cols="10" rows="10"></textarea>
<input type="submit" />
</form>
</body>
</html>

Posted: Tue Sep 12, 2006 8:06 am
by nadavvin
Now It's work. The problem is that I use id instead of name

Code: Select all

<html>
<head><meta/><title></title></head>
<body>
<form action="textToHTML.php" method="post">
<textarea name="content" cols="10" rows="10"></textarea>
<input type="submit" />
</form>
</body>
</html>

THanks for your help.

Why the command line didn't work?

Code: Select all

$ php -r "$a = 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf' . PHP_EOL . 'asdf'; var_dump(preg_split('#^#m', $a, -1, PREG_SPLIT_NO_EMPTY));"

Parse error: syntax error, unexpected '=' in Command line code on line 1

Posted: Tue Sep 12, 2006 8:44 am
by feyd
I can't say for sure without knowing more about what version you're running, like I've already asked.

Posted: Tue Sep 12, 2006 8:54 am
by nadavvin

Code: Select all

php --version
PHP 5.1.2 (cli) (built: Sep  6 2006 22:04:21)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies

Posted: Tue Sep 12, 2006 11:30 am
by nadavvin
I have another problem:

I want to replace html tags to view only but It's not work.

Code: Select all

<?php
$content=$_POST['content'];
$result;
preg_replace("/</","<",$content);
preg_replace("/>/",">",$content);
#$regex='/^|\n.*\n|$/m';
$regex = '#^#m';
$array=preg_split($regex,$content);
foreach($array as $value){
	$result.="<p>$value</p>";
}
echo $result;
?>
What is the problem?

Posted: Wed Sep 13, 2006 2:12 am
by nadavvin
Now It's work.

The problem was that preg_replace don't change the content and only return string with its replacment:

Code: Select all

<?php
$content=$_POST['content'];
$result;
$content=preg_replace("/</","<",$content);
$content=preg_replace("/>/",">",$content);
#$content=str_replace("<","<",$content);
#$content=str_replace(">",">",$content);
#$regex='/^|\n.*\n|$/m';
$regex = '#^#m';
$array=preg_split($regex,$content);
foreach($array as $value){
	$result.="<p>$value</p>";
}
echo $result;
?>