0

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;
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by mgranger96
0

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(' ', '-', '_', ',', '-');

Edited by smantscheff: n/a

0

$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 '-'.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.