Unstoppable Domains

Replacing [b] with <b>, but then shortening the string...

Spaceship Spaceship
Watch

Albino

Munky DesignsEstablished Member
Impact
17
... results in <b sometimes, or <b> etc, making the rest of the page bold.

for instance, if I had:

'test <b>test</b>', and I used

substr('test <b>test</b>', 0, 7);

I would get:

'test <b'

which makes everything else on that page. pretty much, is there anyway to stop this? im thinking the only way is to not filter and change the s etc when I am stripping the text, otherwise, I always run the possiilty of having some broken html on that section :(
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Apologies if I've misunderstood the question, but wouldn't str_replace() do the job?

HTML:
<?php
$mystr = 'test [b]test[/b]';
$mystr = str_replace('[b]','<b>',$mystr);
$mystr = str_replace('[/b]','</b>',$mystr);
echo $mystr;
?>

Doh! I did misunderstand. You want to get the first X chars or keep going until a closing html tag right?

If you don't need to keep the html tags, you could use:

substr(strip_tags('test <b>test</b>'), 0, 7);
 
Last edited:
0
•••
This is untested but the theory I think should do what you want?

PHP:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//	STRING PLACEHOLDER ( ALREADY SHORTENED AND [b] CONVERTED TO [/b]
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
$string = 'test <b>test</';

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//	FIND HOW MANY OPENING TAGS
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
preg_match_all( '/<b>/i' , $string  , $match );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//	COUNT HOW MANY OPENING TAGS WE HAVE
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
$started = count( $match[0] );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//	COUNT THE ENDING TAGS
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
preg_match_all( '/<\/b>/i' , $string  , $match );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//	COUNT HOW MANY OPENING TAGS WE HAVE
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
$ended = count( $match[0] );

if( $started != $ended AND $started > 0 )
{
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //	LOOK FOR BROKEN TAGS
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  if( preg_match( '/<\/?b?>?$/i' , $string , $match ) )
  {
  	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  	//	REPLACE THE BROKEN STRING WITH A CORRECT ONE
  	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  	$string = str_replace( preg_quote( $match[0] ) , '</b>' , $string );
  }else{
  	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  	//	ADD THE ENDING STRING
  	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  	$string = $string.'</b>';
  }
}

If your converting other bbcode as well you'll have to explode them out by the tags and see which one doesn't end then add it in.

Cheers,

Jay
 
0
•••
Easiest way would be to shorten the string with substr(), and then parse the BB-Code
 
0
•••
Todd, im not quite sure what you are looking for exactly.

Are you simply wanting to replace and with <b> and </b> respectively, although it is not working for you?

If so, why not just use the str_replace function? It will do the job just as fast, and will not result in cutting off any text (wont shorten the string)...

Rhett.
 
0
•••
sorry, my request was a really hard one to put into words lol.

I'll try again, a bit clearer.

I have some text, which I parse with bb code ( is bold, italic, etc). Then, for the main page, I display only 250 characters of the text. If, at the cut off point, there happens to be a <br />, for instance, then it will chop the tag in half, resulting in a <b being displayed, and everything thereafter being bold.

I think I have a solution. Once parsed and shortened, I check the amount of <. if there is an odd number, I close it. That still leaves the option open, however, of only parsing one tag, (resulting in a <b>), and everything still being bold.

tis a tricky one!
 
0
•••
nevermind, misunderstood
 
Last edited:
0
•••
Dynadot — .com TransferDynadot — .com Transfer

We're social

Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back