Jump to content

substr line count - instead of character count


alexcmm

Recommended Posts

Hello, I was given this code that works great, which cuts off text after so many characters, and I can add a little "more..." link to see the whole thing.

 

What I'd like to find is code that lets me limit how many lines show, but doesn't care about characters.

 

This is the code I have already... would it be close to this?

<?
MYSQL_CONNECT(localhost, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$database") or die( "Unable to select database"); 

$query="SELECT * FROM $table ORDER BY id DESC limit 1";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$announ=mysql_result($result,$i,"announ");

$short = substr ($announ, 0 , 165); 
$count = strlen($short);
$link = "... <i><a href='main_announ.php?id=$id' class=\"text_small\">[more...]</span></a></i>";

if($count < "165") {
echo $short;
} else {
echo $short . $link;
}
$i++;
}

?>

 

Thanks for your help!

Link to comment
Share on other sites

At the risk of being kicked off of this forum, I have to say...

I always seem to get about 40-50% help from this place on my issues. I'm am not very well versed in PHP and need help, that's why I'm here. Every answer I get seems to be rushed and only half the story. Then when I need more help I end up just being ignored. I seriously thought about donating money to this forum, but I'm waiting to have an issue actually get resolved. I had some things early on that were real easy that made it through to resolution, but even that was like pulling teeth.

 

For example, I saw another persons post that asked how to sort by date. There were a few reply questions "gathering more info" that were just pointless. I mean this is so easy that I knew the answer, and I could have explained it in one reply. Probably because I know how the newbie mind works. It seems obvious to me that if someone is asking a simple question like that, they may need to see an example of what it looks like or some explaination of what some of the compontents mean. Anything else is going to seem cryptic to a newbie.... like myself.

 

I'm guessing I'll have some "really nice" replies to this message, but I really feel like that needed to be said.

 

If you all are really wanting to help, then do it all the way, and not just as much as you feel like before dropping it.

 

If anybody is interested in helping me still, I would greatly appreciate it. I've already given my code. The reason was so someone could see how I need to work my issue into my existing code. If someone could look and help show me what my code SHOULD look like that would be great.

 

Thanks,  alexcmm

Link to comment
Share on other sites

Some people (myself included) would rather help a user help themselves. Giving out complete solutions gets the problem solved, but does very little to teach most users anything.

 

One of the biggest things you need to learn to program is how to research. Take a hint on a forum as an opertunity for you to learn more, don't consider it as being unhelpfull.

Link to comment
Share on other sites

I was in the process of writing some code to do this for you, but when I read the smart ass reply you put up there, I deleted it, I told you what you could do, and what you needed to do it, wordwrap every certain amount of characters with a line break, then do substr_replace to cut off everything after so many lines, finding lines by each line break.

Link to comment
Share on other sites

See, there was nothing smart ass about my post. I was not sarcastic or rude. Just frustrated and telling you exactly what my issue was. You saying it was smart ass, just takes the wind out of your sails. I carefully considered before I posted this that it would piss people off. But I thought, if they knew they could help more with the same or less effort, maybe they would hear that. And if they don't... I'm not really getting what I need here anyway. My goal wasn't to upset... but to show you that I'

m not being helped like I could be

 

And there has never been a time that I have either found a piece of code or been given code, that I didn't disect it as much as I possibly could. I don't just take it, use and never look back. I want to understand this. But my last thread was simply dropped after a while. I'm sorry... I treid reading the PHP.net pages about that issue, as well as this one. I need to see it in action to understnd it best. That's how I learn. I know you're trying to teach people to fish here... but that method just hasn't  been working for me lately. I need a little more.

 

If you don't want to help me (Halofreek)... that's fine. Nothing I can do about it. But you're proviong my point that if I don't ask a question just the right way, or try to decipher partial answers, I'm going to be blown off by you... the knowledgable community. And it makes you look like elitists. But that can't be true, right? Why would you be here helping people like me.

 

Just be thoughtful that people like me feel like they're being blown off instead of helped. If you don't care that I'm being made to feel that way... ok. Enough said. I won't count on you anymore for help. And maybe that's how you want it.

 

(And I never assumed anyone got paid for thier help here. I do know that donations are requested to help pay for overhead, though.)

Link to comment
Share on other sites

I've had countless threads dropped here, simply becuase the question wasn't something people saw fit to answer.  It happens, it sucks, but it happens, and when it does, you just gotta deal and figure it out.

 

You know how to use substr_replace to limit by characters, right?  Do strpos to find the position of the beginning of the break in the line that you want, then tell it to cut off everything after it with substr_replace.  This, should help:

 

<?php
$str = "ABCDEFGHIJKLMNOPQRSTUVWQYZabcdefghijklmnopqrstuvwqyz0123456789012ABCDEFGHIJKLMNOPQRSTUVWQYZabcdefghijklmnopqrstuvwqyz0123456789012";
$charsperline = 13;
$lnbreak = "<br />";
$str = wordwrap($str, $charsperline, $lnbreak, true);
echo $str;
$lines = 8;
$offset = (($charsperline * $lines) + (strlen($lnbreak) * ($lines - 2)) + 1);
$search = strpos($str, $lnbreak, $offset);
$str = substr_replace($str, "...", $search);
echo "\n\n".$lnbreak.$lnbreak.$str;
?>

 

I tested this, it outputs this:

 

ABCDEFGHIJKLM
NOPQRSTUVWQYZ
abcdefghijklm
nopqrstuvwqyz
0123456789012
ABCDEFGHIJKLM
NOPQRSTUVWQYZ
abcdefghijklm
nopqrstuvwqyz
0123456789012 

ABCDEFGHIJKLM
NOPQRSTUVWQYZ
abcdefghijklm
nopqrstuvwqyz
0123456789012
ABCDEFGHIJKLM
NOPQRSTUVWQYZ
abcdefghijklm...

 

Of course, you will need to add some checks in to make sure you don't split a word in half, but this will get you started.  Basically, you have your string, you put a linebreak ever $charsperline (in this case, 13) with wordwrap, then you tell it that you want $lines (in this case 2,) and you find the $offset (starting point) for your $search, which returns the starting point for your substr_replace, which truncates everything after the $search character's position, replacing it with a "...".

 

Keep in mind, this is designed to be used to actually truncate the string.  Since there are by default 4 lines here, when I put 4 as $lines, it prints just a ..., so be careful.

 

I hope this helps you, and in the future, don't try to guilt trip people into doinf this for you, I am happy to help people with this stuff, but I have a job at which I work almost all day, I simply don't usually have time to sit down and write this stuff for you on your time.

 

Edit: I just updated the math a little bit, I found out that it didn't work properly for really long lines when you are printing more than just a few lines, becuase it didn't add the length of the line break that you add, so it would fall behind after a while, I fixed it though, it works now...at least, it should.

Link to comment
Share on other sites

See, there was nothing smart ass about my post. I was not sarcastic or rude. Just frustrated and telling you exactly what my issue was. You saying it was smart ass, just takes the wind out of your sails. I carefully considered before I posted this that it would piss people off. But I thought, if they knew they could help more with the same or less effort, maybe they would hear that. And if they don't... I'm not really getting what I need here anyway. My goal wasn't to upset... but to show you that I'

m not being helped like I could be

 

And there has never been a time that I have either found a piece of code or been given code, that I didn't disect it as much as I possibly could. I don't just take it, use and never look back. I want to understand this. But my last thread was simply dropped after a while. I'm sorry... I treid reading the PHP.net pages about that issue, as well as this one. I need to see it in action to understnd it best. That's how I learn. I know you're trying to teach people to fish here... but that method just hasn't  been working for me lately. I need a little more.

 

If you don't want to help me (Halofreek)... that's fine. Nothing I can do about it. But you're proviong my point that if I don't ask a question just the right way, or try to decipher partial answers, I'm going to be blown off by you... the knowledgable community. And it makes you look like elitists. But that can't be true, right? Why would you be here helping people like me.

 

Just be thoughtful that people like me feel like they're being blown off instead of helped. If you don't care that I'm being made to feel that way... ok. Enough said. I won't count on you anymore for help. And maybe that's how you want it.

 

(And I never assumed anyone got paid for thier help here. I do know that donations are requested to help pay for overhead, though.)

I'm truly sorry that that's how you feel.  However, you need to comprehend the name of the forum "PHP Help".  Nowhere in here does it say that we write code for you.  While we can give examples, keep in mind not everyone can do that.  Some people know the conceptual ideas, but not enough to write the code, and you should respect their help as much as you would HaLo's who actually gave you an example.  What you said was far out of line.  If you want code written for you, go to PHP Freelancing.  We're not here to write scripts, we're here to help with problems about your current script.  And if you wanted an example, why not say it.  Here would be a good post you could have made:

 

"I was recently given this piece of code that cuts a string down to a certain amount of characters.  What I am wondering is if it is possible to modify the below code to have it count a certain amount of lines.  I don't quite understand how this should be done, so I'd really appreciate it if someone could give me an example."

Link to comment
Share on other sites

True.  Very true.

 

I had some free time and I knew how to do this, so I did it, it took about 10-15 minutes to write the above code, then debug it, and I wouldn't have done it if I didn't have the time, I work today, I just have an hour before I have to be there and I wanted to help out.

Link to comment
Share on other sites

If I am reading the original post correctly, you just want to break it at new lines, not caring about how many characters but only wanting a certain amount of lines right?

 

<?php
$string = "this is\n a bunch \n of \n new \n lines in this \n string";

//Let's say you only want the first 2 lines
$stringArr = split("\n", $string, 2); // limit the split to 2 lines

$newString = implode("<br />", $stringArr);
print $newString;

$extraString = splitBreaks($string, 2); // splits 2 breaks.

// create that into a function
function splitBreaks($string, $howmany) {
    //Let's say you only want the first 2 lines
    $stringArr = split("\n", $string, 2); // limit the split to 2 lines

    return implode("<br />", $stringArr);
}
?>

 

Hope that is what you are looking for.

Link to comment
Share on other sites

this is the way i'd do it:

<?php
        $str = "ALKJSDOIASDAlkjadliAJSDLASDioaJFLADASd908123qALKDJAs0-9812elkASJF912e";
        $chars_per_line = 10;
        $num_lines = 2;

        $str = wordwrap($str, $chars_per_line, "FLAG");
        $str_array = explode("FLAG", $str);

        for($i = 0; $i < $num_lines; $i++){
                echo $str_array[$i] ."<br />\n";
        }
?>

 

 

EDIT:

frost, i was thinking the same thing, but i wasn't sure what the string would look like. i assumed it would be separated by '\n' delimiters but i wasn't sure, so i came up with that^^^ instead. if that is the case, you can replace 'FLAG' with '\n' and it will still work.

Link to comment
Share on other sites

Why not:

<?php
function showLines($string, $howMany) {
     return implode("\n", explode("\n", $string, $howMany));
}
?>

 

AH, i forgot you can provide a limit for explode! i like that much better. and i wasn't sure if you can use explode as an argument for implode. sometimes when you nest native functions php shits the bed. fantastic work Glyde!

Link to comment
Share on other sites

Meh, I lied, it should be this:

function showLines($string, $howMany) {
return implode("\n", array_slice(explode("\n", $string, $howMany + 1), -1));
}

Well, for starters, the limit function apparently tells PHP how many array keys to make, not actually how many times to split the string, hence the +1 in the limit.  Secondly, I forgot explode returns the rest of the string in the last key, hence the array_slice.  Anyways, works like a charm; and it is actually tested.

Link to comment
Share on other sites

I'm sorry if you feel I was out of line. I was just trying to be to the point and didn't mean to imply anything else. I guess I should have started with "thanks for all of you help up until now, but..."

 

Please realize that this wasn't my first or even second thread that was dropped(ignored) after a certian point - before resolution. If I realized this is just how it is sometimes, I wouldn't have posted what I did.

 

I am truly thankful for all of the help given up until this point. I'm sorry I wasn't able to take the information given, then find the solution on my own. Again, I never expected complete code to be written for me. That's not the case at all. In fact, in my last post (before todays post) I just wanted and example of one little snippet of code.

then do substr and count how many line breaks you have, and limit to only, 2 line breaks

I don't mind figuring things out. I enjoy the hell out of it when I actually make something work. I just needed more after looking around to see if I could make sense of it.

 

I hope you can see that I've posted here quite a few times before and I've always been exceedingly polite.

 

Anyway, I offer my apologies for my part in this. Ironically, this has sparked a lot of help for me, huh!!  =) Thank you all for your time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.