1

i get different string (like a, b, hospital, schools, jobs to work etc. ) as $divname from my db in my PHP section. i use this values as id in mydiv elemnents.

<div id="'.$divname.'"></div>

there is no problem for $divname = a, b, hospital or school but when it comes to jobs to work there is a huge problem cause my id gets spaces and it returns an error to me.

<div id="jobs to work"></div> //as evryone state spaces in id variable is an error.

now my question i need use this $divame variables in my id attribute. how can i do this? how can i delete those spaces or any more idea for using those in id attributes are welcome.

user3301042
  • 352
  • 2
  • 14
  • use htmlentities maybe ? http://ca3.php.net/manual/en/function.htmlentities.php – Maximus2012 Feb 28 '14 at 17:50
  • 1
    or you can replace the space with underscore or something before assigning the names to the div elements. – Maximus2012 Feb 28 '14 at 17:51
  • possible duplicate of [How to strip all spaces out of a string in php?](http://stackoverflow.com/questions/2109325/how-to-strip-all-spaces-out-of-a-string-in-php) – user3301042 Feb 28 '14 at 18:07

2 Answers2

1

You may do two things:

  1. Use a hashing function to create new id, which will be unqiue as long as your ids are unique as:

    $newdivid = md5($olddivid);
    
  2. You may write a function to remove spaces and combine such string items

    function remove_spaces($str) {
       $str_arr = explode(' ', $str);
       $newstr = "";
       foreach($str_arr as $sitem){
           $newstr .= trim($sitem);
       }
       return $newstr;
    }
    

Hope this solves your problem.

akm
  • 830
  • 6
  • 20
0

i found a php command in another Sof question for this purpose

$new_divname = str_replace(' ', '', $divname);

it deletes all spaces.. and of course my question gets a dublicate:

Community
  • 1
  • 1
user3301042
  • 352
  • 2
  • 14