tags:

views:

75

answers:

6

I am trying to add a .html suffix at the end of all strings supplied by the user before it is entered into the database. Here's my code so far:

strtolower(str_replace(" ", "_", $postTitle));

The above takes the title of the post the user it trying to make, turns it to lower case, replaces all white spaces with underscores and makes it ready to be entered into a database column called post_url.

I just need it to do one more thing and that's to add a .html at the end of each post url. What is the function to do this?

+1  A: 
$postTitle = strtolower(str_replace(" ", "_", $postTitle));    
$postTitle .= '.html';

.= is the Concatenation and assignment operator in PhP. It will add the string '.html' on to the end of $postTitle and then set the value of $postTitle equal to it. If you simply want to concatinate two strings together than you can do it like this:

$string3 = $string1.$string2;
Daniel Bingham
+1  A: 

maybe I don't get your question, but what about this:

strtolower(str_replace(" ", "_", $postTitle)).'.html';
Jochen Hilgers
duh! I knew it was something obvious, thanks.
Golem
easy points. ;)
knittl
+1  A: 
$newName = strtolower(str_replace(" ", "_", $postTitle)) . '.html';

You mention "white spaces", though you're only actually catching spaces, not all types of white space. Based on what you've said about the particular problem, this is probably enough, but make sure you give consideration to whether tabs ("\t") and/or newlines & linefeeds may be present as well ("\n", "\r").

keithjgrant
Thanks for the tips about the additional space types :)
Golem
+1  A: 

whould:

strtolower(str_replace(" ", "_", $postTitle)).".html";

do the trick?

Kasia Gogolek
+5  A: 

hey,

really simiple answer:

$url_string = strtolower(str_replace(" ", "_", $postTitle)).'html';

but, you need to consider security and escaping bad characters for the url.

You need to remove and non alpha-numeric chars by doing this you solve two problems

  1. Possible SQL injection

  2. invalid urls

e.g

// remove all bad chars from string e.g. commas, single quotes etc
$url_string = preg_replace(“/[^a-zA-Z0-9\s]/”, “”, $postTitle);
// replace spaces with underscore and make all lowercase 
$url_string = strtolower(str_replace(" ", "_", $url_string));
// add .html to end of string.
$url_string = $url_string.'html';
Derek Organ
got it, thanks. I will use preg_replace from now on. Security wasn't that big an issue because the user was the admin at all times but this will be helpful when making forms used by the public.
Golem
to be honest, "security wasn't that big an issue because... " is a very lame excuse. how long will it be that way and frankly its what gets forgotten when someone else takes over and now you have a security exploit and a bug in the system because urls won't work.
Derek Organ
`url_encode()` should be helpful, too.
keithjgrant
yea that would work but I find the urls will look a bit ugly then.
Derek Organ
+1  A: 

This is a smoother solution in my opinion:

function handle_create($string) {
  $healthy = array(' ', 'å', 'ä', 'ö');
  $yummy = array('-', 'a', 'a', 'o');
  $string = str_replace( $healthy, $yummy, mb_strtolower( $string, 'UTF-8' ) );
  $string = preg_replace('/-{1,}/', '-', $string );
  return preg_replace('/[^a-z0-9\-\/]/i', '', $string) ;
}

$title = 'Lörem ipsum dålor sit amet - consectetur adipiscing elit.';
echo handle_create( $title );

Would result in

lorem-ipsum-dalor-sit-amet-consectetur-adipiscing-elit

jwandborg