Welcome Guest, Not a member yet? Register   Sign In
Help with strings
#1

[eluser]k2zs[/eluser]
I know I've seen it somewhere before but can't seem to find it now. I need to take a _POST field and convert it from mixed case to lower case and replace spaces with dashes

"Some Text" becomes "some-text"
#2

[eluser]nuwanda[/eluser]
Code:
function get_slug($string){

  $slug = preg_replace('/[^A-Za-z0-9-]+/', '-', strtolower($string));
    
  return $slug;

}

That makes a slug.

That regex says, anything that is not an uppercase or lowercase character or number or dash, convert to a dash.

Yes, I know that CI has a slug function, but it was inconsistent for me. Something to do with not parsing dots. So I use the above.

I have simple needs.

;-)
#3

[eluser]nuwanda[/eluser]
Actually, you could do without the A-Z in the regex since I've already converted it to lowercase on the right side.

See, answering question actually helps your own code. ;-)
#4

[eluser]John_Betong[/eluser]
Try this:

Code:
$this->load->helper('url');

  $formatted_string =  url_title($_POST['unformatted_string']);

 
 
 
#5

[eluser]k2zs[/eluser]
[quote author="nuwanda" date="1290695218"]
Code:
function get_slug($string){

  $slug = preg_replace('/[^A-Za-z0-9-]+/', '-', strtolower($string));
    
  return $slug;

}

That makes a slug.

That regex says, anything that is not an uppercase or lowercase character or number or dash, convert to a dash.

Yes, I know that CI has a slug function, but it was inconsistent for me. Something to do with not parsing dots. So I use the above.

I have simple needs.

;-)[/quote]

That did the trick... Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB