Click to See Complete Forum and Search --> : preg_replace problem easy to regex person


Markbad311
02-20-2006, 07:46 PM
I have a preg_replace problem. I am trying to get this statement to replace all instances of "St. Patrick's Day" to a link to my patties day page!


$details = preg_replace('/\w(St. Patrick\'s Day?)\w/i', "<a href=\"erie-bars-directory/st_patrick\'s_day_erie.php\">$1</a>", $details);

is this right because it don't do anything?

NogDog
02-20-2006, 08:22 PM
See if this works better for you:

$details = preg_replace('/\b(St\. Patrick\'s Day)\b/i',
"<a href=\"erie-bars-directory/st_patrick\'s_day_erie.php\">$1</a>",
$details);

ShrineDesigns
02-20-2006, 08:36 PM
$details = preg_replace("/(st\.[\r\n ]+patrick\'s[\r\n ]+day)/i", "<a href=\"erie-bars-directory/st_patrick\'s_day_erie.php\">\\$1</a>", $details);

Markbad311
02-21-2006, 10:31 AM
I have to replacement going on for my $details Variable. one makes band or bands event or events and Erie bold... The other is your example. The St. Patrick's Day preg_replace doesn't work yet I tried both examples. The other preg_replace is formatting some URLs too. With Strong tags right in a URL! lol machine's have no mercy :( any ways maybe adding white space or telling no double quotes can be in front of it or something?


$details = preg_replace('/\w(erie|bands?|events?)\w/i', "<strong>$1</strong>",
$details);


$details = preg_replace('/\b(St\. Patrick\'s Day)\b/i',
"<a href=\"erie-bars-directory/st_patrick\'s_day_erie.php\">$1</a>",
$details);



in this enviorment:
<?php
$page_title = "Erie Scene Home Page";
require("listingsfunc.php");
if ($_GET['id'] != "") {
dbconnect();
sidemenu($connection);
showsingle($connection);
} if ($_GET['show_all_erie_pa_bars']) {
dbconnect();
showall($connection);
} else if (!$_GET['id']) {
dbconnect();
eventreg($connection);
sidemenu($connection);
}

pagecount($page_title);
$details = preg_replace('/\w(erie|bands?|events?)\w/i', "<strong>$1</strong>", $details);
$details = preg_replace('/\b(St\. Patrick\'s Day)\b/i',
"<a href=\"erie-bars-directory/st_patrick\'s_day_erie.php\">$1</a>",
$details);

mysql_close($connection);
?>




check this little mishap, in a URL I need to be able to stop this from happening. with the <strong> tag preg_replace


<a href="%3Cstrong%3Eerie%3C/strong%3E-%3Cstrong%3Ebars%3C/strong%3E-directory/st_patrick%27s_day_erie.php">
</a>
<!-- Suppose to be-->
<!-- <a href="erie-bars-directory/st_patrick's_day_erie.php">-->


Any ideas? I have been reading about regular expressions but haven't got any to work. lol

NogDog
02-21-2006, 12:14 PM
Use \b for word boundary, not \w (which is any "word character".

Markbad311
02-24-2006, 03:45 PM
I still get this:
http://www.eriescene.com/%3Cstrong%3Eerie%3C/strong%3E-pa-%3Cstrong%3Eevents%3C/strong%3E/%3Cstrong%3Eerie%3C/strong%3E-%3Cstrong%3Eband%3C/strong%3E-spooner-placebus.php

When I use this replace


$details = preg_replace('/\b(erie|bands?|events?)\b/i', "<strong>$1</strong>", $details);
$details = preg_replace('/\b(St\. Patrick\'s Day)\b/i',
"<a href=\"erie-bars-directory/st_patrick\'s_day_erie.php\">$1</a>",
$details);


The "St. Patrick's Day" preg_replace still does not work either.