homepage Welcome to WebmasterWorld Guest from 54.237.54.83
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
substr(stripslashes(trim - Trimming at the next sentence
Trying to insert tag at next sentence rather than at specified character.
hantousha




msg:4251075
 9:49 pm on Jan 9, 2011 (gmt 0)

I have a code see below:

<b><?= stripslashes($newstitle)?></b><br><br />

<b><?= stripslashes($detrsltnewsrow['sourceagency'])?></b>&nbsp;- &nbsp;<?=$ndt?>

<br>

<?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),0,701)?>

</td></tr>

<tr><td colspan="2" class="para" style="padding-left:10px;"><?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),701)?></td></tr>


It inserts the tag
</td></tr><tr><td colspan="2" class="para" style="padding-left:10px;">
at the break of the 701 character. I want it to insert this at the end of a sentence at above 701 character of a description text. The way i have it it breaks the word if it hits the 701 character in the middle of the word. Will like it to insert the tags above at the next sentence after the 701 character. Any assistance.. will be appreciated...

 

alias




msg:4251317
 2:37 pm on Jan 10, 2011 (gmt 0)

I created this bit, and am using it everywhere since:

$longstring = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque faucibus urna non velit elementum eu molestie quam posuere. In non risus sem, id convallis ligula. Proin felis erat, adipiscing vel dapibus tempus, gravida eu purus. Proin lacus eros, adipiscing cursus malesuada a, consectetur ac mi. Sed eros elit, commodo ut fermentum non, mattis ac mauris. Mauris a risus convallis est condimentum accumsan vitae sagittis erat. Maecenas vestibulum imperdiet vulputate. Donec sed quam sit amet velit malesuada tincidunt. Etiam auctor dignissim egestas. Aliquam sed neque eros. Vivamus tellus lorem, consectetur sit amet pellentesque in, porta venenatis tellus. Morbi vitae augue eget dui rhoncus gravida. Ut eget odio in lacus pharetra pellentesque.';
$out = preg_replace('/^(.{10}[^.]*).*/i','$1.',$longstring);
echo $out; // Lorem ipsum dolor sit amet, consectetur adipiscing elit.


{10} specifies the minimum length of the trim you need (that would be {710} in your case), and after that it goes on until it sees a full stop [^.]
You can add more characters here, that would trigger a cut, like [^.,-_:\s]

hantousha




msg:4251552
 10:01 pm on Jan 10, 2011 (gmt 0)

Thanks Alias...

Though how do I put it in this line of tag.. I tried different ways and I keep getting an error.. in the

<?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),0,701)?>

hantousha




msg:4251589
 11:33 pm on Jan 10, 2011 (gmt 0)


<?
$out = preg_replace('/^(.{701}[^.]*).*/i','$1.',$detrsltnewsrow[news_desc]);
echo $out;
?>

</td></tr>

<tr><td colspan="2" class="para" style="padding-left:10px;"><?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),701)?></td></tr>


OK. I figured out the way to output it.. Now how do we have it to continue from where it stopped.. in the next substring that Shows:
<?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),701)?>


So basically I want to start another output starting from the next sentence after the character 701 int he text. Basically a continuation of the above statement $out

alias




msg:4251717
 9:44 am on Jan 11, 2011 (gmt 0)

In that case you'll have to do it a bit differently:

$longstring = 'First sentence. Second sentence. Third sentence.';
$out = preg_match('/^(.{10}[^.]*)(.*)/i', $longstring, $matches);
print_r($matches);


That will print you out an array, $matches[0] being the whole string, $matches[1] being the first part of the string, $matches[2] being the rest of the string (with a dot in the beginning, which you should be able to take care of yourself without any problems I believe).

alias




msg:4251718
 9:47 am on Jan 11, 2011 (gmt 0)

Oops, a quick note - you don't need to assign $out like I did in the example before, so it's just this:

$longstring = 'First sentence. Second sentence. Third sentence.';
preg_match('/^(.{10}[^.]*)(.*)/i', $longstring, $out);
print_r($out);

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved