Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 5:43 am
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 12, 2006 6:43 am
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
[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"
}
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 6:56 am
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:
Why $value is empty?
I get the same result with your regex:
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 12, 2006 7:06 am
Delimiters are always required.
preg_split() removes the pattern it finds, this is the same behaviour as explode()
you're using $_post not $_POST
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 7:13 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 12, 2006 7:18 am
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
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 7:22 am
feyd wrote: Delimiters are always required.
preg_split() removes the pattern it finds, this is the same behaviour as explode()
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()
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 12, 2006 7:29 am
Are you sure $_POST['content'] is what you think it is?
Code: Select all
echo '<pre>' . var_export($_POST['content'], true) . '</pre>';
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 8:02 am
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
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>
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 8:06 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 12, 2006 8:44 am
I can't say for sure without knowing more about what version you're running, like I've already asked.
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 8:54 am
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
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Tue Sep 12, 2006 11:30 am
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?
nadavvin
Forum Commoner
Posts: 68 Joined: Wed Sep 06, 2006 6:05 am
Post
by nadavvin » Wed Sep 13, 2006 2:12 am
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;
?>