请教 php str_place("\r\n","\n",$myText)语法出错

jellyzi 发布于 2012/07/23 16:54
阅读 326
收藏 0
PHP
 <?php

	//The text to justify
	
	$myText =
	"But think not that this famous town has
	only hardpooneers, cannibals,and
	bumpkins to show her visitors. Not at
	all. Still New Bedford is a queer place.
	Had it not been for us whalemen, that
	tract of land would this day perhaps
	have been in as howling condition as the
	coast of Labrador."
//语法出错
	$myText= str_replace("\r\n","\n",$myText);
 	$lineLength=40;//The desire line length
	$myTextJustified="";
	$numLines=substr_count($myText,"\n");
	$startOfLine=0;
	
	//Move through each line in turn
	
	for($i=0;$i<$numLines;$i++){
		$originalLineLength=strpos($myText,"\n",$startOfLine)-$startOfLine;
		$justifiedLine=substr($myText,$startOfLine,$originalLineLength);
		$justifiedLinelength=$originalLineLength;
		
		//Keep adding spaces between words util the desired
		//line length is reached
		
		while($i<$numLines-1&&$justifiedLineLength<$lineLength){
			for($j=0;$j<$justifiedLineLength;$j++){
				if(($justifiedLineLength<$lineLength)&&($justifiedLine[$j]==" ")){
					$justifiedLine=substr_replace($justifiedLine," ",$j,0);
					$justifiedLinelength++;
					$j++;
				}
			}
		}
		
		//Add the justified line to the string and move to the
		//start of the next line
		
		$myTextJustified.="$justifiedLine\n";
		$startOfLine+=$originalLineLength+1;
	}

?>

刚刚入门PHP,一边敲书上的代码,发现在DW里老是说str_place语法出错,检查了书上的还是不懂哪里出错,求指教

以下是问题补充:

@jellyzi:2楼是出错的图片~ (2012/07/23 17:03)
加载中
0
皮总
皮总

个人觉得:

 

<?php

//The text to justify

$myText = "
But think not that this famous town has
only hardpooneers, cannibals,and
bumpkins to show her visitors. Not at
all. Still New Bedford is a queer place.
Had it not been for us whalemen, that
tract of land would this day perhaps
have been in as howling condition as the
coast of Labrador.
";

jellyzi
jellyzi
之前用 $myText =<<<END_TEXT But think not that this famous town has ………………………………………………………… ……as howling condition as the coast of Labrador. END_TEXT; 会报错
0
justjavac
justjavac
<span style="color:#e53333;"><b>$myText= str_replace("\r\n","\n",$myText);//DW提示语法chu</b></span>    $lineLength=40;//The desire line length

这是什么意思?如果是你源代码,很明显,语法错误。如果是你粘贴的问题,那先把这个修正了,回头再求答案。
jellyzi
jellyzi
第一次发~修改好了
0
jellyzi
匿名t3a
匿名t3a
上面少了个;号
0
justjavac
justjavac
同意 @皮总 的观点,here doc的话,不能缩进。
jellyzi
jellyzi
明白~谢谢
0
皮总
皮总
$myText = <<<END_TEXT 
But think not that this famous town has ………………………………………………………… ……as howling condition as the coast of Labrador. 
END_TEXT;

这样不会出错, 注意好规律!

jellyzi
jellyzi
好的~谢谢
皮总
皮总
<<<END_TEXT 后面不能接东西了
返回顶部
顶部