Forum Moderators: coopster

Message Too Old, No Replies

Changing dynamic link URLs to SE friendly with preg_replace

Works with underscore, but not a hyphen

         

synergy

6:49 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



I'm attempting to change a series of dynamically created links to more SE friendly links using preg_replace.

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 "&#45;" but it doesn't work either.

Thank you.

SeanW

6:52 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



The hyphen has meaning in a regexp, try escaping it with a backslash, ie \- and see how that works.

Sean

synergy

11:17 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



Thanks for the reply but this did not seem to work.

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

synergy

11:53 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



Ok I've come up with a hacked solution to the problem.

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

omoutop

6:47 am on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi!
u can always use htaccess and mode rewrite to do all this without the need of coding and functions
:)

grandpa

8:03 am on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I got this to work. What is /e being used for in your expression? I'm guessing you want to capitalize each word.

$output = preg_replace('/\s+/', '-', $title);

<works for underscore or dash>

ergophobe

4:09 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A few little clarifications....


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.