Jump to content

Pulling the first 150 characters from a MySQL database


jimmyelewis

Recommended Posts

I need to pull the first 150 or so characters from a MySQL database.  I only want link tags to show up in the substring of the database.  What I coded so far basically looks from the 150th character to some offset from that in the string without tags and searches the string with links tags looking for that.  This works unless in between the 150 character to the offset a link occurs.  Does anyone know how to fix this or a better way to go about doing this?

 

Example that would be false:

  stringWithoutTags(150th +offset):"this is just an example"

  stringWithLinkTags:"...this is just <a href="">just an example<a>"

 

                                        $infoLength = 150;				// amount of chars to show in news list
				$stringWithOutTags = strip_tags($a[5]);
				$stringWithLinkTags = strip_tags($a[5],'<a>');
				if(strlen($stringWithOutTags)>$infoLength){
					$infoLength = strpos($stringWithOutTags,' ',$infoLength-1);	//go to the next space offset of $infoLength so a word isn't cut off
					for($i=20;$i>0;$i--){  //how much to string to compare to the string with link tags
						if(strlen($stringWithOutTags)>($infoLength+$i)){
							$offset = $i;		//sets the amount of the string to compare to
							$i = 0;
						}
					}
					$linksLength = strpos($stringWithLinkTags,substr($stringWithOutTags,$infoLength,$offset)); //finds the first post of the space plus the comparing part of the string with links tags to the string with out tags
					if($linksLength===false) $linksLength = strlen($stringWithLinkTags); //Problem here if the substring happens to over lap a link tag.
				}
				else{
					$linksLength = strlen($stringWithLinkTags);
				}
				$stringWithLinkTags = substr($stringWithLinkTags,0,$linksLength);

 

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.