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
  • 31,517
  • 8
  • 49
  • 87
SHT
  • 700
  • 4
  • 18
  • 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 Barkati
  • 2,532
  • 6
  • 25
  • 33
Prashant G Patil
  • 927
  • 1
  • 8
  • 22
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
  • 896
  • 7
  • 16
0

Use

str_replace() and `strtolower()`

to remove space and to convert string to lowercase.

dikesh
  • 2,977
  • 2
  • 16
  • 26
0

PHP version:

$string = strtolower(str_replace(' ', '', $string));
sunshinejr
  • 4,834
  • 2
  • 22
  • 32
0
$artist_url = strtolower(str_replace(' ', '', $artist_formatted_name));
Oleg
  • 67
  • 2
0
$string = strtolower(str_replace(' ', '', $string));
Davide Bellone
  • 89
  • 2
  • 10
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
  • 814
  • 7
  • 9