Now Place:2uzhan.com » Mod Rewrite / Removing Spaces from Database Query

Mod Rewrite / Removing Spaces from Database Query

Web Development @ March 10, 2011   Views: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;


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

I'm attempting to simplify my fundraising page, and would like to show exactly how much each donation would help us out. I've listed the options below, but I would like ...

I'm currently doing some encoding work to protect a site from XSS attacks. We've stuck a bunch of <script>debugger;</script> tags in the db. Dispite my best efforts, the strings I've ...

    //The php section of the code


           function getJSONFromDB($sql){

           $conn = mysqli_connect("localhost", "root", "","rent");

           //echo $sql;

           $result = mysqli_query($conn, $sql)or die(mysqli_error());


           while($row = mysqli_fetch_assoc($result)) {



               return json_encode($arr); ...
© 2018 2uzhan.com Contact