Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eregi_replace not working 2

Status
Not open for further replies.

sinbadly

Technical User
Mar 16, 2006
126
GB
I want to bring an excerpt from the beginning of a text file onto the webpage as a blurb. I want to remove the html coding from it.

I'm using eregi_replace, but it isn't removing the code.

Be grateful if an expert could say what I am doing wrong, please.
Code:
$result = @mysql_query("SELECT  id, head,LEFT(story, 220) as story, DATE_FORMAT(updated, '%d/%m/%y %H%i') as ud, nombre 	FROM inside ORDER BY id DESC LIMIT 3 ");
								$story = $result['story'];
$story = eregi_replace('<div class=\"dropcaps\">', '', 
$story );							
$story = eregi_replace('</div>', '', $story );			
$story = eregi_replace('<div class=\"boxed\">', '', 
$story ); while( $row = mysql_fetch_array($result))	
{	
echo								
'<h1 class="home">'						
. $row["head"] 							
. ' </h1>'	
. $row["story"]							
. ' ... '							
. ' <a href="inside/inside.php?id='				
. $row["nombre"] 						
. '" title="Read more here.">More &raquo;</a></p>';

}
 
i'm not sure whether you are going about this the right way. regular expressions should generally be avoided if there are alternative methods available to you (as per the manual).

with line numbers your code looks like this
Code:
1  $story = $result['story'];
2  $story = eregi_replace('<div class=\"dropcaps\">', '', $story );                            
3  $story = eregi_replace('</div>', '', $story );            
4  $story = eregi_replace('<div class=\"boxed\">', '', 
5  $story ); while( $row = mysql_fetch_array($result))

my comments:

1. this never works. your mysql_query returns a resource handle. you then need to get at the results through a mysql_fetch_* or mysql_results() function. your code should fail here (ie throw a warning)
2. this should work but it would be cleaner to do a str_replace
3. ditto. but this will delete ALL closing div tags, not just the one that closes the dropcaps
4. ditto 2

i'm sure you can fix number 1!

the others - if you want to strip all tags within the excerpt why not use strip_tags (
however if all you care about is styling, why not just add a css declaration before your html output that goes something like this

Code:
.dropcaps, .boxed {display: in-line;}
to force it all inline (or whatever other styling you want).

lastly ... in your actual output code, you are echoing the vlaue of $row['story'] and not the manipulated version of that variable which you storing in $story.
 
Phew, thanks, Justin.

What I need is for the text in the field to appear without the tags it has - dropcaps etc.

So is it that I could use <div style="display: in-line;"> for the formatting and that would let the text display without the formatting?

Cheers.

 
assuming you've fixed the other points ... i was thinking of making use of the cascading nature of stylesheets to fix the problem.

so imagine your output as follows - you fix the style issue simply with a wraparound element and an extra declaration.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"><head>[/URL]
  <style type="text/css">
    .dropcaps {
	color:#FF0000;
	font-size:24px;
	}
	.shortformstory .dropcaps {
		font-size:10px; 
		color:blue;
	}
  </style>
</head>
<body>
	<div class="dropcaps">
 		Some text goes here
	</div>
	<h1>
		now let's override the style sheet 
	</h1>
	<div class="shortformstory">
		<div class="dropcaps">
			the story excerpt goes here
 		</div>
 	</div>
</body>
 
I see. Yes, that looks excellent. I should get the chance to work with it in the next couple of days. Really good of you to go to all that trouble, Justin. All the best paul
 
Well, it does some very interesting things to the page, but doesn't exactly give the look that is desired.

At first, I thought the php function that removes html characters would be the thing, but then I learned that it changes the characters into &things; and that wouldn't help.

The articles in the db are all formatted - and almost the same in the first paragraph. I want to take a short beginning of each (say that first paragraph) and remove all the formatting, so that it appears as 220+/- characters of pure text.

The option - which I have been doing - is to copy the text without html on it, and store it in a separate field. But I'm sure this is not the way that professionals would do it.

Each of the articles has a droppedcap and some have that <div class="boxed"> div. So I thought it oughtn't be too difficult to trim that away ...

 
try strip_tags() on the string. that takes out all html tags. if you want to maintain certain tags you can add them as the second parameter of the function.
 
Strip_tags(). Aha. Will try that and report back. Cheers, Justin.
 
but i still think that manipulating the style sheet is a better option! if the page is over spacing then try setting the relevant styles to display:inline
 
Um, I wonder if I'm not making myself very clear. Sorry, if this is the case.

The drop caps et al work very well in the proper place. Instead of writing a blurb about each article (as I do currently) I want to have just a sentence or so appear to tempt the reader in.

In this position, as a pseudo-blurb, I don't want the first letter to be a versal. And I don't want any tags to be working there.

I was trying with
Code:
SUBSTRING(story, 33, 200) as story
and that seemed most promising. Only now I have to get the first letter out of the dropcaps div ...
 
don't think you do. just change the dropcap div to ignore the dropcap. the key is to wrap your excerpt in a named div. then you declare an extra class

Code:
<style>
.dropcap {//your dropcap declarations}
.wrapperdiv .dropcap {//undo the dropcap declarations}
</style>
<div class="wrapperdiv">
  //output your excerpt here
</div>
 
Looks brilliant, Justin. Thank you. One thing, though, is that the line in the articles with the dropcaps has this marking ...
Code:
<p><div class="dropcaps">W</div>hat Mr Adie advises his clients is ....

So won't I have to get rid of that '</div>' somehow?
 
nope...

what is the style declaration for dropcaps?
 
Actually, having a div there makes your code invalid anyway. You might want to change it to span or strong or something along those lines. But one way or another, via css styling you can make the tag virtually invisible to the user.
 
where's the invalidity Vragabond? are you not allowed divs within p's? or is it that the div becomes a bit pointless (over a span) when you make it display:inline?
 
Paragraph (<p>) cannot hold other block level elements. Divs, lists, tables, forms, none of them should ever be found inside a paragraph.
 
Won't work properly without that <p> there though, Vragabond. At least, that's the way it was. However, all noted, and will try that.

Justin, strip_tags() works perfectly. There's just one problem left now, and that's if I say LEFT(story, 200), often part of a word shows. I wonder if there's anything - other than substring() - that I could use to ensure a whole word is used, please?

But, I have to say, that strip_tags() is amazing! Now I know for certain that PHP and MySQL belong to the black arts. Many thanks.
 
Sorry, Dark Arts, I mean, for fear of causing offence.
 
Vagrabond, many thanks about the span instead of div. Will follow that from now on. Cheers.
 
hmmm ... what you're asking for is probably more intelligence than is typically built into rdbms.

i would do something like the following (assuming you have 200 chars of data in the variable $story

Code:
$pos = strrpos(trim($story), " "); //find last occurrence of space. trim first
//we know that length is 200
$story = substr($story, 0, $pos) . " ...";  //this takes the left most chars up to the last space and adds an ellipsis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top