Jump to content


This topic is now archived and is closed to further replies.


need to replace old tags entered into DB with new.

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>
<a href="http://www.SomeRealEstateCo.com/sales/viewlistings.php?listing=310590">
click here for Some Real Estate Co.</a>
<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:

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?

$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]

Share this post

Link to post
Share on other sites
You have to use (capturing parenthesis) which create \backreferences to what they matched.


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);


Share this post

Link to post
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.



Share this post

Link to post
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.

Share this post

Link to post
Share on other sites


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.