View Full Version : Text string to multiple lines
NXArmada
09-18-2008, 07:32 PM
I have the follow code, but all it outputs is F
<?php
$str_value = "Feel free to use the regular|expression we made above on your own site to validate|email addresses, or modify it for your own purposes.";
for($i=0; $i<sizeof($str_value); $i++) {
$line = trim($str_value[$i]);
$arr = explode("|",$line);
$pg = 3600;
$pg += 100;
$text = "". implode($arr) ."";
echo $text ."<br>\n";
}
?>
It should output:
Feel free to use the regular
expression we made above on your own site to validate
email addresses, or modify it for your own purposes.
Any ideas?
magicyte
09-18-2008, 07:41 PM
Because instead of the = sign next to $arr, you should have put the += sign, I believe.
-magicyte
NXArmada
09-18-2008, 07:52 PM
Because instead of the = sign next to $arr, you should have put the += sign, I believe.
-magicyte
I get this:
Fatal error: Unsupported operand types
magicyte
09-18-2008, 08:25 PM
Uh, well I see what you mean when you say it prints "F". I tried it out, and it actually prints out "F" plus an added <br> (linebreak). I have one question. What is $pg for? BTW, I am a little bit new on PHP, but I CAN help, so what do ALL of these BUILT-IN functions do, ex explode, implode, trim, etc.?
-magicyte
P.S. VERY IMPORTANT!! I just noticed something I did wrong. PHP does allow the += sign. Sorry! Pre-define the $arr like this:
$arr;
then do this:
$arr += explode("|",$line);
That may work. Or, instead of +=, .= might work.
djr33
09-18-2008, 08:30 PM
+= is allowed in PHP. It is, however, only for mathematical operations and has nothing to do with the script above. (unlike JS, you would use .= for strings.)
One major problem here, and I don't know why it isn't giving you an error, is that you're not using implode('|',$arr);
implode, I believe, requires a first operator of the "glue" to reconnect the strings. Feel free to use "" as an empty string if you want.
However, overall, that doesn't do what you want. Using a for loop generally requires that you know how many splits you have first, or at least that you're using a value inside the loop to at some point change and terminate the loop. Neither is the case. It's surprising it isn't an infinite loop.
Anyway, here's something that will work:
Version 1:
<?php
$str = 'abc|def|ghi';
//simplest, just replace:
$str = str_replace('|',"\n",$str);
echo $str;
///OR
//easy foreach loop:
$str = explode('|',$str);
foreach ($str as $ln) {
echo $ln."\n";
}
///OR
//a more complex for loop:
$str = explode('|',$str);
for ($i=0;$i<count($str);$i++) {
echo $str[$i]."\n";
}
///OR
//if you must avoid using explode:
while (strpos($str,'|')!==FALSE) {
echo substr($str,0,strpos($str,'|'))."\n"; //echo everything before the first |
$str = substr($str,strpos($str,'|')+1); //replace the string with everything after the first |
}
echo $str."\n"; //echo the last bit, outside of the loop, since there is no | now
NXArmada
09-18-2008, 08:31 PM
the $pg is not used just something i put in to plan ahead if i can get this to work correctly.
http://us.php.net/manual/en/function.explode.php
http://us.php.net/manual/en/function.implode.php
http://us.php.net/manual/en/function.trim.php
NXArmada
09-18-2008, 08:32 PM
+= is allowed in PHP. It is, however, only for mathematical operations and has nothing to do with the script above. (unlike JS, you would use .= for strings.)
One major problem here, and I don't know why it isn't giving you an error, is that you're not using implode('|',$arr);
implode, I believe, requires a first operator of the "glue" to reconnect the strings. Feel free to use "" as an empty string if you want.
Daniel I did what you suggested and no change.
NXArmada
09-18-2008, 08:34 PM
$arr += explode("|",$line);
yeah that what i did when you first suggest += and the error is still the same
Fatal error: Unsupported operand types
magicyte
09-18-2008, 08:34 PM
Check out my post. I edited it. I have no whereabouts of what glue does, so, Daniel, I ask for help :o.
-magicyte
NXArmada
09-18-2008, 08:40 PM
Check out my post. I edited it. I have no whereabouts of what glue does, so, Daniel, I ask for help :o.
-magicyte
if i do .= i get this
Warning: implode() [function.implode]: Bad arguments.
NXArmada
09-18-2008, 08:47 PM
Okay Daniel the code you provided:
$str = explode('|',$str);
for ($i=0;$i<count($str);$i++) {
echo $str[$i]."\n";
}
Works great now is there a way to do using
printer_draw_text($handle, $specship, 1, 3600);
So that each line that your code generates goes into its own printer_draw_text()
NXArmada
09-19-2008, 01:23 PM
Okay i got my last question answered my self. so here is what my final code looks like. Provided for learning purpose.
<?php
$int_y_pos = 3600;
$int_y_step_small = 100;
$str_value = "Feel free to use the regular|expression we made above on your own site to validate|email addresses, or modify it for your own purposes.";
$handle = printer_open("Brother HL-5150D");
printer_set_option($handle, PRINTER_MODE, text);
printer_set_option($handle, PRINTER_TEXT_ALIGN, PRINTER_TA_LEFT);
printer_start_doc($handle, "Invoice");
printer_start_page($handle);
$str = explode('|',$str_value);
for ($i=0;$i<count($str);$i++) {
$int_y_pos += $int_y_step_small;
printer_draw_text($handle, $str[$i], 1, $int_y_pos);
}
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);
?>
This code allows you to take a string and break it up to multiple lines for PHP's print function. In my case (not shown above) every where the code picks up a </p> (generated by TinyMCE) it breaks it into a new printer_draw_text() function for printing straight to a printer (Windows Only).
magicyte
09-19-2008, 09:17 PM
Cool!
-magicyte
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.