Jump to content

Recommended Posts

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.

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/185653-help-with-substr/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/185653-help-with-substr/#findComment-980324
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/185653-help-with-substr/#findComment-980333
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/185653-help-with-substr/#findComment-980346
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.