在PHP代码中拆分多个连接词-不起作用
在这个URL中,我找到了一个用perl编写的完美的源代码,但我的需求是用PHP编写的 我从未使用过perl,甚至一次也没有,但我已经成功地将perl代码翻译成PHP 但是它没有给出正确的结果,你能帮我找出这个问题吗在PHP代码中拆分多个连接词-不起作用,php,algorithm,perl,function,text-parsing,Php,Algorithm,Perl,Function,Text Parsing,在这个URL中,我找到了一个用perl编写的完美的源代码,但我的需求是用PHP编写的 我从未使用过perl,甚至一次也没有,但我已经成功地将perl代码翻译成PHP 但是它没有给出正确的结果,你能帮我找出这个问题吗 #!/usr/bin/perl use strict; my $WORD_FILE = '/usr/share/dict/words'; #Change as needed my %words; # Hash of words in dictionary # Open dict
#!/usr/bin/perl
use strict;
my $WORD_FILE = '/usr/share/dict/words'; #Change as needed
my %words; # Hash of words in dictionary
# Open dictionary, load words into hash
open(WORDS, $WORD_FILE) or die "Failed to open dictionary: $!\n";
while (<WORDS>) {
chomp;
$words{lc($_)} = 1;
}
close(WORDS);
# Read one line at a time from stdin, break into words
while (<>) {
chomp;
my @words;
find_words(lc($_));
}
sub find_words {
# Print every way $string can be parsed into whole words
my $string = shift;
my @words = @_;
my $length = length $string;
foreach my $i ( 1 .. $length ) {
my $word = substr $string, 0, $i;
my $remainder = substr $string, $i, $length - $i;
# Some dictionaries contain each letter as a word
next if ($i == 1 && ($word ne "a" && $word ne "i"));
if (defined($words{$word})) {
push @words, $word;
if ($remainder eq "") {
print join(' ', @words), "\n";
return;
} else {
find_words($remainder, @words);
}
pop @words;
}
}
return;
}
#/usr/bin/perl
严格使用;
我的$WORD_文件='/usr/share/dict/words'#根据需要改变
我的%words;#字典中的单词散列
#打开字典,将单词加载到哈希中
打开(WORDS,$WORD\u文件)或死亡“无法打开字典:$!\n”;
而(){
咀嚼;
$words{lc($)}=1;
}
近(字);
#从stdin中一次读一行,分成几个单词
而(){
咀嚼;
我的文字;
查找单词(lc($);
}
子查找单词{
#打印所有可以将$string解析为完整单词的方式
我的$string=shift;
我的"文字",;
my$length=长度$string;
foreach my$i(1..$length){
my$word=substr$string,0,$i;
my$rements=substr$string,$i,$length-$i;
#有些词典把每个字母都当作一个词来写
下一个if($i==1&&($word ne“a”&&$word ne“i”);
if(已定义($words{$word})){
推送@words,$word;
如果($余数相等“”){
打印连接(“”,@words),“\n”;
回来
}否则{
查找单词($余数,@words);
}
pop@words;
}
}
回来
}
PHP代码是我写的,但不起作用
<?php
$WORD_FILE = file_get_contents ("word.txt") ;
$words = Array () ;
foreach ($WORD_FILE as $str)
{
$words [$str] = 1 ;
}
while(true)
{
$input = trim(fgets(STDIN, 1024));
find_words ($input) ;
}
function find_words ($str)
{
$string = $str ;
$length = strlen ($str) ;
for ($i = 1 ; $i <= $length; $i++)
{
$word = substr ($string, 0, $i) ;
$remainder = substr ($string, $i, $length - $i) ;
$i++ ;
if ($i == 1 && ($word != "a" && $word != "i")) ;
if ($words($word))
{
array_push($words, $word) ;
if ($remainder == "")
{
print_r ($words) ;
return ;
}
else
{
find_words ($remainder, @words) ;
}
array_pop ($words) ;
}
}
return ;
}
?>
您的foreach($WORD\u文件为$str)
需要有一个数组来代替$WORD\u文件
,因此它应该是这样的:
$WORD_FILE = file_get_contents("word.txt");
$words = explode(" ", $WORD_FILE);
foreach ($words as $str) {
*commands go here*
}
file\u get\u contents仅返回字符串。您可以使用分解将该字符串基于另一个字符串分解为数组。在本例中,我使用了
“
表示一个空格。它的工作副本,不确定其中是否有错误。
如果有人能找出错误,请告诉我
<?php
$WORD_FILE = file_get_contents ("word.txt") ;
$temp_arr = explode("\n", $WORD_FILE);
foreach ($temp_arr as $str)
{
$words [$str] = 1 ;
}
$processed = Array () ;
find_words ($argv[1]) ;
print_r ($processed) ;
function find_words ($str)
{
global $words ;
global $processed ;
$string = $str ;
$length = strlen ($str) ;
for ($i = 1 ; $i <= $length + 1; $i++)
{
$word = substr ($string, 0, $i) ;
$remainder = substr ($string, $i, $length - $i) ;
if ($i == 1 && ($word != "a" && $word != "i")) ;
if (array_key_exists ($word, $words))
{
array_push($processed, $word) ;
if ($remainder == "")
{
return ;
}
else
{
find_words ($remainder, $words) ;
}
echo "popping the word " . array_pop ($words) . "\n";
}
}
return ;
}
?>
不过,这个问题被否决了。但我相信这会对其他人有用。