Jump to content

[SOLVED] PHP newbie question - pulling specific data out of a string...


ShootingBlanks

Recommended Posts

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!!!

 

Link to comment
Share on other sites

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!...

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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;

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.