Jump to content

Need Help on Reg Exp


kks_krishna

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.