Forum Moderators: coopster
The code works great... except when I try to use a hypen in the URL as opposed to an underscore. It falls apart. Any ideas?
Here is the php code:
function makeUrlFriendly($title) {
// Replace spaces with hypens
$output = preg_replace("/\s/e" , "_" , $title);
// Remove non-word characters
$output = preg_replace("/\W/e" , "" , $output);
return strtolower($output);
} And the HTML/PHP echo line:
echo "<img src=\"./images/bullet.gif\" alt=\"\" /><a href=\"".makeUrlFriendly("$title").".htm\">".stripslashes($title)."</a><br /><br />"; This outputs the URL of the link as "name_of_article_title.htm". I want it to be "name-of-article-title.htm".
As mentioned above, simply changing the "_" to "-" doesn't work... it causes the following errors:
----------------------------------------
Parse error: parse error, unexpected ';' in /home/blah/public_html/articles2.htm(76) : regexp code on line 1
Fatal error: preg_replace(): Failed evaluating code: - in /home/blah/public_html/articles2.htm on line 76
----------------------------------------
I've also tried using the ASCII hyphen value "-" but it doesn't work either.
Thank you.
$output = preg_replace("/\s/e" , "\-" , $title); Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/blah/public_html/articles2.htm(76) : regexp code on line 1
Parse error: parse error, unexpected ';' in /home/blah/public_html/articles2.htm(76) : regexp code on line 1
Fatal error: preg_replace(): Failed evaluating code: \- in /home/blah/public_html/articles2.htm on line 76
Any other ideas on how to get preg_replace to place hyphens instead of underscores?
I discovered that you can use str_replace to replace underscores with hyphens. So, once the spaces are converted to underscores, I then convert the underscores to hyphens.
Here's how to make a URL friendly (with hyphens) using PHP:
function makeUrlFriendly($title) {
// Replace spaces with underscores
$output = preg_replace("/\s/e" , "_" , $title);
// Remove non-word characters
$output = preg_replace("/\W/e" , "" , $output);
// replace underscores with hyphens
$output = str_replace("_" , "-", $output);
return strtolower($output);
}
The hyphen has meaning in a regexp,
Only in character classes and never in replacement groups, so...
preg_replace('/[a-z]/', '*', 'This is a string');
// hyphen has special meaning. Output: T*** ** * ******
preg_replace('/a-z/', '*', 'This is a string');
// hyphen has no special meaning. Output: This is a string
preg_replace('/is/', '-', 'This is a string');
// hyphen has no special meaning. Output: Th- - a string
In other words, that should not have been causing any problems for synergy and therefore,
Any other ideas on how to get preg_replace to place hyphens instead of underscores?
There's no difference between hyphens and underscores in replacement groups. You seem to have left off a semi-colon when you rewrote it for hyphens or something.
u can always use htaccess and mode rewrite to do all this without the need of coding and functions
mod_rewrite affects what input will map to a given underlying URL, but it does not change how internal links are output in your navigation and so forth. I think synergy is working on the output side of the equation here.