ShootingBlanks Posted November 14, 2007 Share Posted November 14, 2007 Hello. Let's say I have a string of data that I've stored in a variable $textSting. Now, I want to make that $textString variable be only the first 20 characters AFTER the 12th character (so, characters 13-33). Here's some other things to note: The string will always have at least 12 characters, so cutting out the first twelve will always be an option The string may not have up to 20 characters that follow the 12th character - there may be any number less than that If the string has less than or equal to 20 characters after the 12th character, then I just need the total rest of the chacaters. So, if there's 30 characters total, I'd want to cut out the first twelve, then display all of the remaining 18. If there are MORE than 20 characters, then after the 20 are displayed, I'd like to tack on "..." to the end of the last character I hope all of this makes sense. I'm pretty new to PHP (have only done a couple random projects), so I'm hoping to learn from any info/code than anyone can provide to me. Thanks so much! - these forums are great!!! Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 $textString = substr ( $textString, 12 ); Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 A fuller, more explicit example with checking might be : $subString = substr($textString, 12, 20); $totalExtraLength = strlen(substr($textString, 12)); if($totalExtraLength > 20){ echo $subString."..."; } else { echo $subString; } Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 That worked great - thanks so much! I checked the PHP reference manual too, so I understand what's going on... One more thing, though... ...what if I wanted to amend the code so that if it encountered the character "<" (the "less than" sign) within the extra 20 characters in the $textString, then even if there WERE more than 20 extra characters, it would just stop there. So, it would make it as though that "<" were making the $totalExtraLength less than 20, even if it really wasn't. If that was confusing - here's an example... $textString = "2007-12-03: This is a string.<br />2007-12-02: This is another string. Now this string is more than 20 characters." Okay, based on the string above, I would only want the following displayed: This is a string. I would not want: This is a string.<br... Is that possible? Thanks!... Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 Change $subString = substr($textString, 12, 20); To : $subString = substr(substr($textString, 12, 20),0,strpos(substr($textString, 12, 20),'<')); Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 My previous post might be rather inefficient, as a result here is the replacement code that for line : $subString = substr($textString, 12, 20); $subString = substr($subString,0,strpos($subString,'<')); Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 If you want to add some flexibility into this (in case something changes in the format you receive) consider the following instead : $startPos = 12; $maxLength = 20; $stopChar = '<'; $subString = substr($textString, $startPos, $maxLength); if(($stopPos = strpos($subString,$stopChar)) === FALSE){ $stopPos = strlen($subString); } $subString = substr($subString,0,$stopPos); note: there is an error is the character doesn't exists in the specified space. This has now been corrected. Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 If you want to add some flexibility into this (in case something changes in the format you receive) consider the following instead : $startPos = 12; $maxLength = 20; $stopChar = '<'; $subString = substr($textString, $startPos, $maxLength); $subString = substr($subString,0,strpos($subString),$stopChar)); I got an error with that about there being an extra ")", so I removed one from the end, and now I'm getting this error: Warning: Wrong parameter count for strpos() in C:\htdocs\_PHP-SITES\ProjectBoard\index.php on line 162 Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 I saw that you edited your last post. Now here's what's happening... * If $totalExtraLength is less than 20 and does NOT have a "<" in it, then nothing is displayed. * If $totalExtraLength is more than 20 and DOES have a "<" in it, then everything before the "<" is displayed, followed by "...". * If $totalExtraLength is more than 20 and does NOT have a "<" in it, then "..." is displayed. So, it's not working. Do you have any other suggestions? Thanks!... EDITED: In the PHP manual, I saw that if the 2nd parameter in strpos is not found (in this case $stopChar), then it will return "FALSE". I'm wondering if that is what is making nothing be displayed in the case that no "<" is found? Quote Link to comment Share on other sites More sharing options...
aschk Posted November 14, 2007 Share Posted November 14, 2007 See my last post. I corrected several problems. Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted November 14, 2007 Author Share Posted November 14, 2007 Based on your help and the PHP Manual - I created a solution. THANKS!!! Here it is: $startPos = 12; $maxLength = 20; $subStringBreak = false; $subString = substr($textString, $startPos, $maxLength); if (strpos($subString,'<')) { $subString = substr($subString,0,strpos($subString,'<')); $subStringBreak = true; } $totalExtraLength = strlen(substr($textString, $startPos)); if ($subStringBreak == false) { if($totalExtraLength > $maxLength){ $finalString = $subString . "..."; } else { $finalString = $subString; } } else { $finalString = $subString; $subStringBreak = false; } echo $finalString; 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.