1

In my MySQL database I have this table which contains multiple artists. They are placed in a field called formated_name and returns strings like for example Lady Gaga, so far so good. What I want to achieve is that it returns ladygaga so I can use it for my url later on. My whole function looks like this:

public function artistname_get($artist_id)  
{ 
    $this->load->database();

    $sql = "SELECT formated_name FROM artists WHERE artist_id = '".$artist_id."'";
    $query = $this->db->query($sql);
    $data = $query->result();

    if($data) {
        $this->response($data, 200);
    } else {
        $this->response(array('error' => 'Couldn\'t find any artist with that name!'), 404);
    }
}

So how can I achieve this?

Sougata Bose
  • 30,169
  • 8
  • 42
  • 82
SHT
  • 680
  • 3
  • 16
  • 39

9 Answers9

1

Try out this simple way.

To remove spaces in string.

By using PHP

$string = 'Lady Ga ga';

$value=str_replace(' ','',$string);

echo strtolower($value);

By using MySQL

$sql = "SELECT REPLACE( formated_name, " ", "" ) FROM artists WHERE artist_id = '".$artist_id."'";
Shahzad
  • 2,443
  • 6
  • 21
  • 31
Prashant G Patil
  • 837
  • 1
  • 6
  • 21
0

Mysql offers string function for this as well if you do not want to do it with php.

ToLower function:

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_lower

Replace to replace spaces:

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

But you should additionally use something for url_encode and url_decode within php. Otherwise you may experiance problems with artists like "A & B"

eX0du5
  • 876
  • 7
  • 15
0

Use

str_replace() and `strtolower()`

to remove space and to convert string to lowercase.

dikesh
  • 2,075
  • 2
  • 13
  • 23
0

PHP version:

$string = strtolower(str_replace(' ', '', $string));
sunshinejr
  • 4,718
  • 2
  • 20
  • 32
0
$artist_url = strtolower(str_replace(' ', '', $artist_formatted_name));
Oleg
  • 67
  • 2
0
$string = strtolower(str_replace(' ', '', $string));
0

Try this,

SELECT LOWER(REPLACE(formated_name, ' ', '')) AS formated_name

You have to do is Just use mysql functions LOWER and REPLACE together to get rid of space and capital letters

0

Try Something like this.....

<?php 
$string = 'Lady Gaga';
$value=str_replace(' ','',$string);
echo strtolower($value);

?>
ssss
  • 76
  • 8
0

If you want to put it in URL you should clear also from special characters etc.

Use this function to convert string into alias.

function toAlias ($string) {
    return strtolower (preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $string)));
}
wake-up-neo
  • 704
  • 5
  • 9