k0z Posted December 18, 2009 Share Posted December 18, 2009 Hi, I have this text in a string: Joe has posted in <a href="xx">a topic</a> I need to truncate this code, so that it only contains a maximum of 46 characters. The problem is, I want to be able to truncate the contents of the link, while still maintaining the link. So for example if the string was: Joe has posted in <a href="xx">abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz</a> It would be able to truncate the whole thing to the 46 characters, but still maintain the link. So the above string woudl return something along the lines of ( i didn't actually count that characters ): Joe has posted in <a href="xx">abcdefghijklmnopqrstuvwxyzabcd</a>... What would the easiest way to do this be? Thanks for your time! EDIT: Note that the html itself should not count as part of the characters when counted. The actual LENGTH is just the text, so in the above example the actual displayed length of text would be: Joe has posted in abcdefghijklmnopqrstuvwxyzabcd ... That part would be the actual 46 chars. note again I didnt actually count that to 46 chars, just estimating. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 18, 2009 Share Posted December 18, 2009 That's easy enough, but I'm not sure how to add the ellipse (i.e. ...) if the length exceeds 46 characters - at least not without making the code more complex. [FYI: The "t" in the second set of letters is the 46th character] $text = 'Joe has posted in <a href="xx">abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz</a> and also in <a href="xx">abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz</a>, plus in <a href="xx">abcde</a>'; $new_text = preg_replace("/<a([^>]*)>?([^<]{1,46})[^<]*<\/a>/", '<a\\1>\\2</a>', $text); echo $new_text; //Output // // Joe has posted in <a href="xx">abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst</a> // and also in <a href="xx">abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst</a>, plus in // <a href="xx">abcde</a> Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 18, 2009 Share Posted December 18, 2009 OK, it was much simpler than I thought to add the ellipses $text = preg_replace("/<a([^>]*)>([^<]{46})[^<]*<\/a>/", '<a\\1>\\2</a>...', $text); Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 18, 2009 Share Posted December 18, 2009 Still playing around with this, just for my own enjoyment. Here is an improved version that will add a title to links with text that is over 46 characters. It makes the title the entire text. So, if the text is truncated the user can mouse over the link to see the entire text in a popup. $text = preg_replace("/<a([^>]*)>([^<]{46})([^<]*)<\/a>/", '<a\\1 title="\\2\\3">\\2</a>...', $text); Quote Link to comment Share on other sites More sharing options...
k0z Posted December 19, 2009 Author Share Posted December 19, 2009 The ENTIRE length of the displayed text needs to be the 46 chars, otherwise it defeats the point of having the code. How would I make the truncated link PLUS the original text equal a total of 46 chars, while truncating the link itself if necessary? Thanks. Quote Link to comment Share on other sites More sharing options...
k0z Posted December 19, 2009 Author Share Posted December 19, 2009 I modified your code, and I now have this: $a = explode('<a', $thestring); $lengthleft = 46 - strlen($a[0]); $text = preg_replace("/<a([^>]*)>([^<]{" . $lengthleft . "})([^<]*)<\/a>/", '<a\\1 title="\\2\\3">\\2</a>...', $thestring); Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 19, 2009 Share Posted December 19, 2009 Your code doesn't work according to your requirments. The ENTIRE length of the displayed text needs to be the 46 chars, otherwise it defeats the point of having the code. How would I make the truncated link PLUS the original text equal a total of 46 chars, while truncating the link itself if necessary? What? You are contradicting yourself. Does the "displayed" text of the link need to be 46 characters (if so your code fails) or do you want the entire code of the link (including the opening and closing tags) to be 46 characters. If it is the latter, that really makes no sense. If the total length of the text gets too bug, then you will have an empty string as the display text and the actual URL would start to get truncated. If you are only wanting the displayed text to be 46 characters or less, then the code I provided does exactly that. Your 'modified' code makes no sense. EDIT: OK after rereading your posts several times and looking closer at your code, I guess I understand what you wanted now. But, it could have been explained better. There is still a better way to do this. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.