Jump to content


Photo

need to replace old tags entered into DB with new.


  • Please log in to reply
4 replies to this topic

#1 dbrimlow

dbrimlow
  • Members
  • PipPipPip
  • Advanced Member
  • 712 posts

Posted 30 August 2006 - 10:12 PM

The NYTimes requires that we add on garbage html to the end of our apartment listings "description" field.

for example, our data entry people have to actually enter the follow html tags into our text description of a listing:

..yada, yda. yada long description, Incredibly low maintenance & taxes
including a 14 year tax abatement! Marketed exclusively by Some Real Estate Co.
<br><br><b><font color="#996699">*September, 2006 Occupancy</font></b> <br><br>
<a href="http://www.SomeRealEstateCo.com/loft_media/tours/bchtourfinal.wmv"> Model Home Virtual Tour</a>
<br><br>
<a href="http://www.SomeRealEstateCo.com/sales/viewlistings.php?listing=310590">
click here for Some Real Estate Co.</a>
<br><br>
<a href="http://www.SomeRealEstateCo.com.com">www.SomeRealEstateCo.com.com</a>

I want to replace the tags and insert the replacements correctly whenever using the description field in our own dynamic pages. I want the
<br><br><b><font color="#996699">*sometext</font></b>
to be:
<p><strong>*sometext</strong></p>

Would I use an array? If so how would I recognize text between the font tags to now be between the opening/closing <p><strong> tags?

<?php
$description = "'<br><br><b><font color=\"#996699\">.*</font>'";
$patterns[0] = "'<br><br>'";
$patterns[1] = "'<b>.*</b>'";
$patterns[2] = "'<font color=\"#996699\">.*</font>'";

$replacements[2] = ".*";
$replacements[1] = "'<strong>.*</strong>'";
$replacements[0] = "'<p></p>'";

echo preg_replace($patterns, $replacements, $description);
?>

Dave

I am always amazed by no matter how much I know, I know I don't know as much as I thought I knew.  - D. Brimlow.

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 31 August 2006 - 04:15 AM

You have to use (capturing parenthesis) which create \backreferences to what they matched.

<pre>
<?php

	echo $string = '<br><br><b><font color="#996699">*sometext</font></b>';
	echo preg_replace('%(?:<br>){2}<b><font[^>]+>(.+?)</font></b>%', '<p><strong>\1</strong></p>', $string);

?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 dbrimlow

dbrimlow
  • Members
  • PipPipPip
  • Advanced Member
  • 712 posts

Posted 31 August 2006 - 07:44 PM

Cool. Thank you. And MAN this is fun stuff.

Now, first question, do I need to actually echo the replace, or can I just redefine the variable to include this expression as in the example below?

//get flyer info
	$query = "Select * from temp where (flyer_id = '$maxid')";
	$result = mysql_query("$query") or die(mysql_error());
	while($myrow=mysql_fetch_array($result)) {

                $description = $myrow[description];
		$description = str_replace("\\", "", $description);
		$description = wordwrap($description, 50, Chr(13));
                $description preg_replace('%(?:<br>){2}<b><font[^>]+>(.+?)</font></b>%',  '<p><strong>\1</strong></p>', $description);
 
                $listing = $myrow[numb];
		$title = $myrow[title];
		... etc, etc, ?>

This way it is corrected as a variable and I can insert it where I want within a webpage and an EOF.

Question 2 - Just to be sure I follow your pattern correctly, is this basically what happened:

1. 
% (?:<br>){2}
= start expression until ended, don't capture or backreference as a subpattern, and any character before the <b> twice is optional and :

1b.
<b><font[^>]+>(.+?)</font></b>%
continue matching, include one or more characters from here on (after <font), then include anything before ">", capture any one or more characters as a subpatten, stop expression,

2. insert subpatter (\1) between
<p><strong>\1</strong></p>
,

3. newly revised variable.

Thanks,

Dave
Dave

I am always amazed by no matter how much I know, I know I don't know as much as I thought I knew.  - D. Brimlow.

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 31 August 2006 - 07:48 PM

I only used echo for sake of example; you do not have to.

(?: ) are non-capturing parenthesis; they do not create backreferences.
{2} is requiring exactly 2 matches of what precedes it.
.+? the ? is making the + non-greedy.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 dbrimlow

dbrimlow
  • Members
  • PipPipPip
  • Advanced Member
  • 712 posts

Posted 31 August 2006 - 07:49 PM

I think I posted before done, but you answered my question. Thanks again.
Dave

I am always amazed by no matter how much I know, I know I don't know as much as I thought I knew.  - D. Brimlow.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users