kks_krishna Posted July 12, 2007 Share Posted July 12, 2007 HI, I have the following code: $link_url_id = preg_replace("/[^a-z0-9-]/","",strtolower(str_replace(" ","-",$link_text))); If I give the input as : Spring / Webwork2 Integration It's giving the following output: spring--webwork2-integration.html Why its showing the multiple "--". I want it to be only one "-". should not be more than one "-" continuously. Please help me. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 12, 2007 Share Posted July 12, 2007 This will work, but I'm sure there is a more efficinet solution: $link_url_id = preg_replace("/-{2,}/","-",preg_replace("/[^a-z0-9-]/","",strtolower(str_replace(" ","-",$link_text)))); Quote Link to comment Share on other sites More sharing options...
kks_krishna Posted July 12, 2007 Author Share Posted July 12, 2007 Thanks, I need one more help from you. " JBoss Seam book makes the JavaOne official best seller list ". If you look the string it has spaces in the start and end. When it converted it looks like this: -jboss-seam-book-makes-the-javaone-official-best-seller-list-.html How can I remove the starting and ending "-" from the string. advance thanks for your help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 12, 2007 Share Posted July 12, 2007 trim() Quote Link to comment Share on other sites More sharing options...
effigy Posted July 12, 2007 Share Posted July 12, 2007 <pre> <?php function urlize($string) { $string = trim($string); $string = strtolower($string); $string = preg_replace('/[^-a-z0-9 ]/', '', $string); $string = preg_replace('/\s+/', ' ', $string); $string = str_replace(' ', '-', $string); return "$string.html"; } echo urlize('Spring / Webwork2 Integration'); echo '<br>'; echo urlize(' JBoss Seam book makes the JavaOne official best seller list '); ?> </pre> Quote Link to comment Share on other sites More sharing options...
kks_krishna Posted July 12, 2007 Author Share Posted July 12, 2007 Great dude. Its working fine now. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 12, 2007 Share Posted July 12, 2007 Not to be contrary, but why not do it all in one line? <?php $string = preg_replace(array( '/^[^a-z0-9]+/', '/[^a-z0-9]+$/', '/[^a-z0-9]+/' ),array( '', '', '-' ),strtolower($string)) . '.html'; // ...or even... $string = preg_replace(array( '/^\W+/', '/\W+$/', '/\W+/' ),array( '', '', '-' ),strtolower($string)) . '.html'; ?> The difference between the two is that the bottom one will preserve underscores (_) instead of turning them into dashes. Also, in neither case will you ever get double dashes, whereas you can in effigy's function if a space and a dash are adjacent in the original string. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 12, 2007 Share Posted July 12, 2007 Personally I prefer effigy's solution as it is more readable and easier to modify. To change it to remove double dashes would be simple enough: <pre> <?php function urlize($string) { $string = trim($string); //trim whitespace from ends $string = strtolower($string); //Convert to lower case $string = preg_replace('/\s+/', '-', $string); //Replace any whitespace with dashes $string = preg_replace('/[^-a-z0-9]/', '', $string); //remove any non-supported characters $string = preg_replace("/-+/","-", $string); //Replace mutiple dashes with a single dash return "$string.html"; } echo urlize('Spring- / Webwork2 - - - Integration'); echo '<br>'; echo urlize(' JBoss Seam book makes the JavaOne official best seller list '); ?> </pre> Quote Link to comment Share on other sites More sharing options...
kks_krishna Posted July 13, 2007 Author Share Posted July 13, 2007 Thanks, Its working like gem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.