I'm working on generating SEO friendly URLs from data taken from a database. I've been successful for the most part. The only issue I'm having is when I stripped the spaces and made them dashes, I lose the data being pulled from the database. If I leave the spaces in there (ex. www.test.com/test/this is a sample article/) it works fine. When I replace the spaces with dashes I lose the data that supposed to be displayed on the page. It also works if I change the spaces to '+' signs. I'd like to have '-' or '_' though.

Query code:

$query = "Select * From Family Where $View = View order by Name";

This is the link code.

echo '<a href="/family/';
$product_title_url = gen_seo_friendly_titles($row['Name']);
echo $product_title_url;
echo '/">';

This is the code to strip out spaces etc.

function gen_seo_friendly_titles($title) {
  $replace_what = array('  ', ' - ', ' ',    ', ', ',');
  $replace_with = array(' ', '-', '_', ',', '-');
  $title = strtolower($title);
  $title = str_replace($replace_what, $replace_with, $title);
  return $title;
}

Recommended Answers

All 2 Replies

What does $View contain in your query?
In these lines the replacement ', ' by ',' can never occur because all blanks have already been replaced before.

$replace_what = array('  ', ' - ', ' ',    ', ', ',');
$replace_with = array(' ', '-', '_', ',', '-');

$View simply contains a number. It acts as a "Active" test, in this particular php file, I'm displaying all the contents of a specific table that are active.

I'm not worried about the ',' because none of the data in this particular instance use commas at all.

I should have mentioned that all of this works perfect if the $row field is just one word. I'm getting no data for $row fields that have multiple words in that field unless I display the new URL as http://www.test.com/this is a test article/ leaving the spaces in. I get no data when replacing the spaces making the new URL http://www.test.com/this-is-a-test-article.

I'm assuming the issue is that the data in that field is not being passed correctly to the new URL because of the dashes. And like I said before if I replace the spaces with '+' signs making the new URL http://www.test.com/this+is+a+test+article/, it works just fine. The only thing not working is the '-'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.