Jump to content

Need help for creating URL using PHP Reg Exp


kks_krishna

Recommended Posts

HI,

 

I want to create urls dynamically when i am inserting the new data to database.

 

ForExample,

 

If the story title is :

 

What is new in Site?

 

It should be converted as :

 

what-is-new-in-site.html

 

It shouldn't allow any special characters in the URL.

 

How can I do this?

I'd make one small change to that:

<?php
  $str="What is new in Site?";
  $new_str=strtolower(str_replace(" ","-",$str));
  echo $str;
  echo $new_str;
?>

 

Best to keep URLs lower case. You'd also want to remove all non allowed characters like the ? as that wouldn't be allowed in the URL. Not exactly sure how that'd be done although I have written this regex string to grab everything except the ?

 

([ a-zA-Z]*)

I dont quite understand.

 

Do you want an error if anything but alpha-numeric and - come up

 

OR

Do you want it just to remove them so

 

HE-LLO$^ M"Y n@A~me IS C-h(oc*o&pi

 

would become

 

HE-LLO MY nAme IS C-hocopi

 

~ Chocopi

 

Do you want it just to remove them so

 

HE-LLO$^ M"Y n@A~me IS C-h(oc*o&pi

 

would become

 

HE-LLO MY nAme IS C-hocopi

 

Yes, I want to remove all..but the spaces will be replaed with "-" and also there should not more than one "-" continuosly like "---". It should be trimed to "-"

 

 

Thanks,

krishna

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.