1

When we use substr to display part of the string but if our string has anchor tag then displayed string content viewing smaller than others strings

e.g.

$str1="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str2="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str1Count=strlen($str1); // 357

$str2Count=strlen($str2); // 393

if($str1Count > 300){
   echo substr($str1,0,300)."<br/><br/>";
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into el
*/


if($str2Count > 300){
    echo substr($str2,0,300);
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five 
*/

but as per my need it has to display till "into el"

please help me thanks in advance.

3
  • 2
    seriously, these were the shortest string examples you could come up with? :/ how about truncating to 20 characters? Commented Dec 7, 2013 at 12:21
  • have you tried echo htmlspecialchars(substr($str2,0,300)); (asuming you are displaying the result in a browser)? Commented Dec 7, 2013 at 12:25
  • htmlspecialchars() not working it displays till "only five" Commented Dec 7, 2013 at 12:32

3 Answers 3

2

you may need to use strip_tags

echo substr(strip_tags($str2),0,300);

Note: Don't forget to use strip_tags when checking the length of the string also.

Sign up to request clarification or add additional context in comments.

Comments

1

This will do the trick but it will remove your hyperlink. do you want to keep hyperlink intact?

<?php   

$str1="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str2="is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It";

$str1Count=strlen(strip_tags($str1)); // 357

$str2Count=strlen(strip_tags($str2)); // 393

if($str1Count > 300){
   echo substr(strip_tags($str1),0,300)."<br/><br/>";
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into el
*/


if($str2Count > 300){
    echo substr(strip_tags($str2),0,300);
}

/* 
output:
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy <a href='http://www.google.com'>text</a> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five 
*/

?>

Comments

0

Use it

function limit_words($string, $word_limit){
    $words = explode(" ",$string);
    return implode(" ",array_splice($words,0,$word_limit));
}

2 Comments

why use this ?? OP don't want to split words.
@bansi this will give the proper result and (string has anchor tag then displayed string content viewing smaller than others strings) problem not occur

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.