Jump to content

need to replace old tags entered into DB with new.


dbrimlow

Recommended Posts

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:

[code]..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>[/code]

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

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?

[code]<?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);
?> [/code]
Link to comment
Share on other sites

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

[code]
<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>
[/code]
Link to comment
Share on other sites

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?

[code] //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, ?>[/code]

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.  [code]% (?:<br>){2}[/code] = start expression until ended, don't capture or backreference as a subpattern, and any character before the <b> twice is optional and :

1b. [code] <b><font[^>]+>(.+?)</font></b>% [/code]
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 [code]<p><strong>\1</strong></p>[/code],

3. newly revised variable.

Thanks,

Dave
Link to comment
Share on other sites

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

[tt](?: )[/tt] are non-capturing parenthesis; they do not create backreferences.
[tt]{2}[/tt] is requiring exactly 2 matches of what precedes it.
[tt].+?[/tt] the ? is making the + non-greedy.
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.