2016-10-28 88 views
0

我是一名初級PHP開發人員。 對於客戶,我需要將一些字符串放入pdf中。我正在使用FPDI,我喜歡它。 我有一個現有的模板PDF,我需要將一個字符串的每個字符插入一個小圖形框(請參閱圖像)。將字符串插入到For循環中

example

每個字符必須從每個人2毫米(8px)。

每個字符串可以有不同的長度,所以我想這樣做:

$name = 'namenamename'; 
$stringcount = strlen($name)-1; 
$countspace = $stringcount*2; 
//121 = coordinate of first box 
for ($x=121; $x <= $x+$countspace; $x = $x+2) { 
    for ($i=0; $i <= $stringcount; $i++) {   
    $pdf->SetXY($x, 37); 
    $pdf->Write(0,$name[$i]); 
    } 
} 

這是行不通的。這是錯誤:

Maximum execution time of 30 seconds

你能幫助我請正確的方法和一個新手很好的解釋嗎? :)

回答

0

試試這個代碼:

 


    $name = 'namenamename'; 
$string_length = strlen($name); 

$coordinate = 121; //Give to the variable coordinate the beginning value, in this case 121 
for ($i=0; $i < $string_length; $i++){ //make only one loop for the string length so the loop ends when there is no more characters 


    $char = substr($name,$i,1); // this is "the tricky part", with substr you can grab each character with its position in the string 
    $pdf -> SetXY($coordinate, 37); // here you put the coordinate for the character 
    $pdf -> Write(0, $char); // write it 
    $coordinate += 2; // and increment it by two, since the character are two spaces away from each other 
} 

希望這將有助於..

+0

真的非常感謝ManoS!這是我第一次使用For Loop,你的回答確實有助於我理解邏輯。 – bttptr

+0

我很高興我幫助你:)沒問題,跟上好工作夥伴,儘可能地嘗試編碼和學習,每一天你會變得更好,那麼你是昨天;) – ManoS

0

也許並不是一個很好的解決方案,但你可以改變的執行時間與這行代碼

的set_time_limit($秒);

無論如何給它一個嘗試,但我認爲這是更多的錯誤循環的邏輯也許。

你能準確地說出你需要兩個第一個字符的座標,第一個是121+還是121?

+0

第一個字符:121。 第二個字符:123 第三個:125 ............. – bttptr

+0

好的我不知道你是否已經解決了它,但我是gonne給你一個例子: ) – ManoS

+0

非常感謝! :D – bttptr