Jump to content

Substr ($variable,x,x) In Echo Statement


jamesjmann

Recommended Posts

echo("<p class=\"commentboxContainer\"><table width=100% border=0>
                      <tr>
                        <td bgcolor=\"#EEE\"><strong><p class=fltlft>$name </strong>says:</p></td>
                      </tr>
                    </table>
                    <table width=100% border=0 bgcolor=\"#EEE\">
                      <tr>
                        <td width=40%><img src=../images/Icons/People/Anonymous.png width=64 height=64 border=1 /></td>
                        <td width=60%>$comment',0,6</td>
                      </tr>
                    </table>
                    <table width=100% border=0>
                      <tr>
                        <td bgcolor=\"#EEE\" ><p class=fltlft><em>$email</em></p></td>
                      </tr>
                      <tr>
                        <td bgcolor=\"#EEE\" ><p class=fltlft>added on $time</p></td>
                      </tr>
                    </table></p><br>"); 

 

Here's my echo statement for displaying comments by users.  I want to add a substr (); function to limit the amount of characters of the $comment field. That way if a user makes a long comment it doesn't push the page down.

 

I tried putting it after the echo ();  but that didn't work. I also tried putting it before $comment and that didn't even work. I know that if I make two separate echo statements, this can be done, but the field "$comment" is displayed within a table that is part of the original echo statement, so what do I do?

Link to comment
https://forums.phpfreaks.com/topic/226644-substr-variablexx-in-echo-statement/
Share on other sites

Ok, half of it is no longer an issue, i.e., breaking it up into three different echo statements doesn't mess up the css.

 

Here's the new code:

echo("<p class=\"commentboxContainer\"><table width=100% border=0>
                      <tr>
                        <td bgcolor=\"#EEE\"><strong><p class=fltlft>$name </strong>says:</p></td>
                      </tr>
                    </table>
                    <table width=100% border=0 bgcolor=\"#EEE\">
                      <tr>
                        <td width=40%><img src=../images/Icons/People/Anonymous.png width=64 height=64 border=1 /></td>
                        <td width=60%>"); 
                        echo substr ("$comment,6");
                        echo ("</td>
                      </tr>
                    </table>
                    <table width=100% border=0>
                      <tr>
                        <td bgcolor=\"#EEE\" ><p class=fltlft><em>$email</em></p></td>
                      </tr>
                      <tr>
                        <td bgcolor=\"#EEE\" ><p class=fltlft>added on $time</p></td>
                      </tr>
                    </table></p><br>"); 

 

The problem I'm having now, though, is with the Substr ();

 

I get the error:

Warning: Wrong parameter count for substr()

 

What did I do wrong?

You're using the substr function in the context of string,6 - Meaning it's just skipping the first 5 characters?

 

Just make sort of like an excerpt, determine a length you want the maximum to be seen, and have a link to read the rest.

 

Something like:

http://danlamanna.com/portfolio/?p=24

Problem solved.

 

I realized that I shouldn't have double quoted the inside of the "echo substr();".

 

I tried using single quotes around the variable next, but I learned that when you add single quotes, your telling the echo statement to display precisely what you write within them, whereas if you leave the variable single/double quote free, it outputs the data contained within the variable.

 

I also scripted the numerical values within the function wrong. I only put ($comment, 6); but it should've been ($comment, 0, 50);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.