0
  while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo " {$row['id']}. ". "<a href=\"/load/{$row['id']}/{$row['name']}\">{$row['name']}</a> <br> ".
     "Music : {$row['lljhg2']} <br> ".
     "Video : {$row['description']} <br> ".
     "Song  : {$row['artist']} <br> ".
     "--------------------------------<br>";}

Some code Like this, I want {$row['name']} in <a href=\"/load/{$row['id']}/{$row['name']}\"> I want this row results all spaces replaced by dash ('-').

If the result is:

Prince Reoy from USA

I want to print this:

prince-reoy-from-usa

Is it possible?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Hâssâñ
  • 26
  • 3
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 25 '15 at 17:49
  • You wanted to say that PDO is more useful than mysql? But, I know nothing about it! – Hâssâñ Aug 25 '15 at 17:53
  • Not more useful, just better. The `mysql_*` API will be removed and one day all of your queries will stop working. There are links in the comment which show you how to use PDO. – Jay Blanchard Aug 25 '15 at 17:54
  • do you want to replace them in a select query, or replace them in your table forever? those are 2 different animals altogether – Funk Forty Niner Aug 25 '15 at 17:58
  • I just want them as they are in the table. but, output with space replaced by dash – Hâssâñ Aug 25 '15 at 18:03
  • 1
    You're trying to glue the data from the columns together? Or is there one column with words that you're trying to glue together? – Jay Blanchard Aug 25 '15 at 18:06
  • OPPS! No, See What i'm trying to say.. $row["name"] What this is I don't really sure. IT just show the result of that tables one print result. But, I want to use that as a get variable in URL so, I need all space removed from that result so that the URL can be user Friendly. But, I'm really can't understand what to do. – Hâssâñ Aug 25 '15 at 18:23
  • If you don't understand, how can you explain it to us so we can help you solve the issue? – Jay Blanchard Aug 25 '15 at 18:34

3 Answers3

1

either use this query

select lcase(replace(name, ' ', '-')) from table_name

or this php code

strtolower(' ', '-', str_replace($row['name']))
mynawaz
  • 1,599
  • 1
  • 9
  • 16
0

You can use implode() which will add the "glue" (the '-') between pieces of the array to make a string. Then you can lower the case of the string all at once with strtolower():

$row_data = strtolower(implode('-', $row));
echo $row_data;
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

This will replace spaces with dashes, and make the whole string lower case:

strtolower(str_replace(' ', '-', $row['name']))

Marcovecchio
  • 1,322
  • 9
  • 19
  • Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Aug 25 '15 at 17:55
  • OK , I'm trying this. – Hâssâñ Aug 25 '15 at 17:56
  • @JayBlanchard: sorry, replaced the "try this" with something better. – Marcovecchio Aug 25 '15 at 17:58
  • "Song URL : {strtolower(str_replace(' ', '-', $row['name']))}{$row['description']}
    ". I used like this but, My page is Showing blank. Can You Please, tell me why?
    – Hâssâñ Aug 25 '15 at 18:02
  • This one will work: "Song URL : ".strtolower(str_replace(' ', '-', $row['name']))."{$row['description']}
    ". The braces will only work when directly dealing with variables, but not on function results.
    – Marcovecchio Aug 25 '15 at 18:06
  • OPPS! No, See What i'm trying to say.. $row["name"] What this is I don't really sure. IT just show the result of that tables one print result. But, I want to use that as a get variable in URL so, I need all spaced removed from that result so that the URL can be user Friendly. But, I'm really can't understand what to do. – Hâssâñ Aug 25 '15 at 18:25
  • Well, `strtolower(str_replace(' ', '-', $row['name']))` does exaclty this: removes whites and makes the string lower case. You can use it as a get value. BUT, if you want to use it as a key to find a database record, that alone won't work. What most devs do is to append the record ID to the normalized string, like this: http://example.com/artists/88491_prince-reoy-from-usa Then you have a semi-friendly URL. The PHP script will have to explode the parameter, getting the number before the underscore, which is the record ID, throwing away the normalized string after the underscore. – Marcovecchio Aug 25 '15 at 18:56